Migration of Spring Security from 3.2.9 to 5.7.1 - spring

I'm trying to migrate Spring Security from 3.2.9 to 5.7.1 and Spring framework from 3.2.13 to 5.3.20.
In the runtime, I'm getting the below errors.
No default constructor found;
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'basicAuth' defined in ServletContext resource [/WEB-INF/security.xml]:
Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.springframework.security.web.authentication.www.BasicAuthenticationFilter]:
No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.security.web.authentication.www.BasicAuthenticationFilter.<init>()
at deployment.mycompany.war//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1334)
at deployment.mycompany.war//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1232)
at deployment.mycompany.war//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at deployment.mycompany.war//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at deployment.mycompany.war//org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at deployment.mycompany.war//org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at deployment.mycompany.war//org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at deployment.mycompany.war//org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at deployment.mycompany.war//org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:330)
... 52 more
Below is the snippet of beans configuration:
<beans:bean id="basicAuth" class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter" >
<beans:property name="authenticationManager" ref="authenticationManager"/>
<beans:property name="authenticationEntryPoint" ref="basicAuthEntryPoint" />
</beans:bean>
Tried to add a constructor initialization for authenticationEntryPoint. It's still not working.
<beans:bean id="basicAuth" class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter" >
<beans:property name="authenticationManager" ref="authenticationManager"/>
<beans:property name="authenticationEntryPoint" ref="basicAuthEntryPoint" />
<beans:constructor-arg index="1" ref="basicAuthEntryPoint" />
</beans:bean>
Appreciate for any help.

The issue was resolved after I made the below changes.
I replaced the below configuration:
<b:bean class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter">
<b:property name="authenticationManager" ref="authenticationManager"/>
<b:property name="authenticationEntryPoint" ref="entryPoint"/>
</b:bean>
With this:
<b:bean class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter">
<b:constructor-arg ref="authenticationManager"/>
<b:constructor-arg ref="entryPoint"/>
</b:bean>

Related

Error creating bean with name AuthenticationManager

I am working on a Spring-MVC application which uses Spring-Security. In the application for login I have to use 2 login url's
/j_spring_security_check_for_person
AND
/j_spring_security_check_for_group
Both the login url's check against the database where I have implemented UserDetails and userDetailsService. To connect these both url's for login, I am using springSecurityFilterChain. Unfortunately, it is not working. I am at this problem since 2 days, I might be making some silly mistake. Kindly check the log and xml,
Error Log :
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authenticationManager': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:239)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1114)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1017)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.security.config.authentication.AuthenticationManagerFactoryBean.getObject(AuthenticationManagerFactoryBean.java:28)
at org.springframework.security.config.authentication.AuthenticationManagerFactoryBean.getObject(AuthenticationManagerFactoryBean.java:20)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)
security-application-context.xml
<security:http create-session="ifRequired" use-expressions="true"
auto-config="true" disable-url-rewriting="true">
<security:form-login login-page="/" default-target-url="/canvas/list"
always-use-default-target="false" authentication-failure-url="/denied.jsp" />
<security:logout logout-success-url="/" delete-cookies="JSESSIONID"
invalidate-session="true" logout-url="/j_spring_security_logout"/>
</security:http>
<bean id="springSecurityFilterChain" class="org.springframework.security.web.FilterChainProxy">
<security:filter-chain-map path-type="ant">
<security:filter-chain pattern="/**" filters="authenticationProcessingFilterForPersonal, authenticationProcessingFilterForGroup"/>
</security:filter-chain-map>
</bean>
<bean id="authenticationProcessingFilterForPersonal"
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<property name="authenticationManager" ref="authenticationManagerForPersonal"/>
<property name="filterProcessesUrl" value="/j_spring_security_check_for_person" />
</bean>
<bean id="authenticationProcessingFilterForGroup"
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<property name="authenticationManager" ref="authenticationManagerForGroup"/>
<property name="filterProcessesUrl" value="/j_spring_security_check_for_group"/>
</bean>
<bean id="authenticationManagerForPersonal" class="org.springframework.security.authentication.ProviderManager">
<property name="providers">
<list>
<bean class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<property name="userDetailsService">
<ref bean="userDetailsService"/>
</property>
<property name="passwordEncoder" ref="encoder"/>
</bean>
</list>
</property>
</bean>
<bean id="authenticationManagerForGroup" class="org.springframework.security.authentication.ProviderManager">
<property name="providers">
<list>
<bean class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<property name="userDetailsService">
<ref bean="groupDetailsService"/>
</property>
<property name="passwordEncoder" ref="encoder"/>
</bean>
</list>
</property>
</bean>
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider ref="authenticationManagerForPersonal"/>
<security:authentication-provider ref="authenticationManagerForGroup"/>
</security:authentication-manager>
<beans:bean id="encoder"
class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
<beans:constructor-arg name="strength" value="11" />
</beans:bean>
If I replace the authentication-manager code with the below snippet, I don't get an error, but the 2 login url's are not registered :
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider user-service-ref="userDetailsService">
<security:password-encoder ref="encoder"/>
</security:authentication-provider>
<security:authentication-provider user-service-ref="groupDetailsService">
<security:password-encoder ref="encoder"/>
</security:authentication-provider>
</security:authentication-manager>-->
authenticationManagerForPersonal and authenticationManagerForGroup are of bean type ProviderManager. This is wrong. You have to declare those beans as type DaoAuthenticationProvider.
Please take a closer look at my spring security configuration.
As the exception message indicates, your authenticationManagerForGroup and authenticationManagerForPersonal are configured invalidly. As discoverable from the type signature and JavaDoc, ProviderManager takes a list of AuthenticationManager as constructor argument, but you configure the AuthenticationManagers as property arguments.
You should be able to get this working by replacing the <property name="providers"> element with <constructor-arg>.

org.springframework.beans.factory.BeanCreationException: Error while mapping Spring with context datasource

This is my spring context file
<!-- <context:property-placeholder location="classpath:jdbc.properties" />-->
<context:component-scan base-package="com.max.premcalc" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/inquizzitiveds"/>
<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean> -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.max.premcalc.domain.PremiumCalc</value>
<value>com.max.premcalc.domain.Inputparam</value>
<value>com.max.premcalc.domain.RiderInput</value>
<value>com.max.premcalc.domain.Rider</value>
<value>com.max.premcalc.domain.Product</value>
<value>com.max.premcalc.domain.Products</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="MainController" class="com.max.premcalc.controller.MainController" />
<bean id = "calcDao" class="com.max.premcalc.dao.CalcDaoImpl"/>
<bean id = "calcService" class="com.max.premcalc.service.CalcServiceImpl"/>
<bean id = "inputparam" class = "com.max.premcalc.domain.Inputparam"/>
But I'm getting this error :
Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private
com.max.premcalc.service.CalcService com.max.premcalc.controller.MainController.calcService; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'calcServiceImpl': Injection
of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: com.max.premcalc.dao.CalcDao com.max.premcalc.service.CalcServiceImpl.calcDao; nested
exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao':
Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void
com.max.premcalc.dao.CalcDaoImpl.setSessionFactory(org.hibernate.SessionFactory); nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in
ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is
org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
You need to set the property
hibernate.dialect
In the hibernate (persistence.xml or bean declaration) configuration, the value depends on your database, for example:
Postgres: org.hibernate.dialect.PostgreSQL82Dialect
Oracle: org.hibernate.dialect.Oracle10gDialect
All posible options are listen here
For example, a sample persistence.xml looks like:
<persistence-unit>
...
<properties>
...
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
...
</properties>
</persistence-unit>
Please, check if your properties file have the key hibernate-dialect
The stacktrace is pretty clear
org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
Apparently hibernate cannot use the metadata to retrieve the type of database to use, as a result one must configure the hibernate dialect explicitly.

Why isn't hibernate creating my tables from hbm.xmls?

I have several .hbm.xml mapping files set up in Spring, and I can tell from the logs that Spring sees them and does something with them, but no tables are created in the DB.
Bean setup:
<beans:bean id="dataSourceEmbedded" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="org.h2.Driver" />
<beans:property name="url" value="jdbc:h2:~/myDbName" />
<beans:property name="username" value="sa" />
<beans:property name="password" value="" />
</beans:bean>
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSourceEmbedded">
<beans:property name="mappingLocations">
<beans:list>
<beans:value>classpath:path/to/schemas/Page.hbm.xml</beans:value>
<beans:value>classpath:path/to/schemas/Navigation.hbm.xml</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:value>
hibernate.show_sql=true
hibernate.dialect=${hibernate.dialect}
hibernate.hbmToDdlAuto=create
</beans:value>
</beans:property>
</beans:bean>
I know that the path to the Page and Navigation mapping files is correct - an error is thrown if it's not, and I can see the fields being mapped during the tomcat startup:
DEBUG: org.hibernate.cfg.Configuration - Processing hbm.xml files
INFO : org.hibernate.cfg.HbmBinder - Mapping class: Page -> pages
DEBUG: org.hibernate.cfg.HbmBinder - Mapped property: Page ID -> pageid
DEBUG: org.hibernate.cfg.HbmBinder - Mapped property: title -> title
Similarly, the database is clearly accessed / locked while tomcat is loading, so something is happening in there! But once it's loaded and I try to query it or inspect it, no tables exist.
Question: Why aren't my tables being created / what have I done wrong?
The property you used is wrong, it should be 'hibernate.hbm2ddl.auto' instead of 'hibernate.hbmToDdlAuto'... However instead of that you can also set the schemaUpdate property of the LocalSessionFactoryBean.
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSourceEmbedded">
<beans:property name="mappingLocations">
<beans:list>
<beans:value>classpath:path/to/schemas/Page.hbm.xml</beans:value>
<beans:value>classpath:path/to/schemas/Navigation.hbm.xml</beans:value>
</beans:list>
</beans:property>
<beans:property name="schemaUpdate" value="true" />
<beans:property name="hibernateProperties">
<beans:value>
hibernate.show_sql=true
hibernate.dialect=${hibernate.dialect}
</beans:value>
</beans:property>
</beans:bean>
Related answer/info: Hibernate hbm2ddl.auto possible values and what they do?
Correct property name is hibernate.hbm2ddl.auto

Spring dataSource and scope

I have project where DB has been moved into claster.
I have to change my scope from singelton into request.
My configuration with sigelton is:
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.userrw}" />
<property name="password" value="${jdbc.userrwpass}" />
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="60"/>
<property name="logAbandoned" value="true"/>
<property name="testOnReturn" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="60000"/>
<property name="testOnBorrow" value="true"/>
<property name="validationQuery" value="SELECT 1"/>
<property name="validationInterval" value="30000"/>
<property name="suspectTimeout" value="60"/>
<property name="jdbcInterceptors" value="org.apache.tomcat.jdbc.pool.interceptor.ResetAbandonedTimer" />
</bean>
When i change:
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close" scope="request">
i get error:
ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'transactionManager' defined in ServletContext resource [/WEB-INF/hibernate-context.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception
is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still
receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:943)
at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:778)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:504)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1385)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:306)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1389)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1653)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1662)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1642)
at java.lang.Thread.run(Thread.java:679)
What is wrong ?
Usually you just need one DataSource instance per application rather than per request. The reason it fails because Spring is trying to initialize DataSource at start up time, at that time there is no request available yet.
So remove scope="request"

No Persistance provider for EntityManager - using Hibernate and Spring MVC

I have Spring MVC project. I am using HSQL database and Hibernate. I have got stuck during making connection with Spring MVC (JPA as default) and Hibernate.
I have created persistance.xml file in Spring MVC directory: src/main/resources/META-INF. I have also tried to place it in Spring MVC directory: Deployed Resources/webapp/WEB-INF/classes/META-INF. I don't know which one is correct. I have still the same problem.
This is my persistance.xml :
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
<persistence-unit name="entityManager">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>package.pl.models.Users</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:/home/user/hsqldb/lib/my.spring.mvc/mybase" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="password" />
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
</properties>
</persistence-unit>
This is one of my Spring MVC controller method body:
UsersHome usersHome = new UsersHome();
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("entityManager");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
usersHome.setEntityManager(entityManager);
Users user = usersHome.findByUsername(login);
I have received error on the line when I have used EntityManagerFactory for the first time - indicating the name of my Persistence Unit.
My exception is:
org.springframework.web.util.NestedServletException: Request processing failed;
nested exception is javax.persistence.PersistenceException: No Persistence
provider for EntityManager named entityManager
And the root cause is:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named entityManager
I have used appropriate name in EntityManagerFactory. Presumably I have done mistake in my Dispatcher Servlet Context file. I have this code according to Hibernate etc.:
<!-- Hibernate session factory -->
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource">
<beans:ref bean="dataSource" />
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
<beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
</beans:props>
</beans:property>
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>package.pl.models.Users</beans:value>
</beans:list>
</beans:property>
</beans:bean>
<!-- Hibernate session factory end -->
<!-- <beans:bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<beans:property name="persistenceUnitName" value="entityManager" />
</beans:bean> -->
<beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory" />
</beans:bean>
<!-- <beans:bean id="usersDao" class="hutter.pl.dao.UsersHome">
<beans:property name="entityManagerFactory" ref="entityManagerFactory" />
</beans:bean> -->
I have commented the bean id="entityManagerFactory" and bean id="usersDao". Are the necessary? However I can't start my web application with uncommented bean id="entityManagerFactory". I am receiving this exception:
org.springframework.beans.factory.BeanCreationException: Error creating bean with
name 'entityManagerFactory' defined in ServletContext resource
[/WEB-INF/spring/appServlet/servlet-context.xml]: Invocation of init method failed;
nested exception is javax.persistence.PersistenceException: No Persistence
provider for EntityManager named entityManager
Sorry, I have not read your full question. I stopped when I saw this: persistance.xml. What about using persistence.xml?
Put your xml file into WEB-INF folder. The xml file contents sessionFactory and all that stub.
And import it into spring.xml file as follows:
<import resource="classpath:persistance.xml" />

Resources