Announcement Announcement Module
Collapse
No announcement yet.
Configure Spring with jpa Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Configure Spring with jpa

    Hi All,
    I got a problem in configuring Spring with Jpa in a J2ee project. I think I need your help. Thanks

    My persistence file:

    HTML Code:
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
    
    	<persistence-unit name="mysqlJPA" transaction-type="RESOURCE_LOCAL">
    		<provider>org.hibernate.ejb.Hibernatepersistence</provider>
    		<!--<class>com.test.entity.Person</class>
    		-->
    		<properties>
    			<property name="hibernate.show_sql" value="true" />
    			<property name="hibernate.format_sql" value="true" />
    			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
    			<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
    			<property name="hibernate.connection.username" value="root" />
    			<property name="hibernate.connection.password" value="###" />
    			<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/rentCar" />
    			<property name="hibernate.max_fetch_depth" value="3" />
    			<property name="hibernate.show_sql" value="true" />
    			<property name="hibernate.hbm2ddl.auto" value="update" />
    		</properties>
    	</persistence-unit>
    </persistence>

    My applicationContext.xml File:

    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
    
    	<bean id="dataSource"
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://localhost:3306/rentCar" />
    		<property name="username" value="root" />
    		<property name="password" value="701701" />
    	</bean>
    	<bean id="entityManagerFactory"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="persistenceXmlLocation" value="classpath*:META-INF\persistence.xml" />
    		<property name="persistenceUnitName" value="mysqlJPA" />
    		<property name="dataSource" ref="dataSource" />
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="showSql" value="true" />
    				<property name="generateDdl" value="true" />
    			</bean>
    		</property>
    	</bean>
    	<bean id="jpaTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    		<property name="dataSource" ref="dataSource" />
    	</bean>
    	<tx:annotation-driven transaction-manager="jpaTxManager" />
    	<bean id="personDAO" class="com.test.dao.PersonDAOImpl" abstract="false"
    		lazy-init="default" autowire="default" dependency-check="default">
    	</bean>
    </beans>
    And my web.xml file

    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
    	xmlns="http://java.sun.com/xml/ns/javaee" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    	
    	 <!-- spring的应用上下文 -->
    	<context-param>
    	  <param-name>contextConfigLocation</param-name>
    	  <param-value>classpath*:applicationContext.xml</param-value>
    	</context-param>
                 <listener>
    	  <listener-class>
    	   org.springframework.web.context.ContextLoaderListener
    	  </listener-class>
    	</listener>
    	<listener>
    		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    	</listener>
    	
    	<!-- OpenSessionInViewFilter过滤器 -->
    	<filter>
    	  <filter-name>lazyLoadingFilter</filter-name>
    	  <filter-class>
    	   org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    	  </filter-class>
    	</filter>
    	<filter-mapping>
    	  <filter-name>lazyLoadingFilter</filter-name>
    	  <url-pattern>*.action</url-pattern>
    	</filter-mapping>
    	
    </web-app>
    At last, my exception statck:

    严重: Context initialization failed
    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'entityManagerFactory' defined in URL [file:/D:/litai/Servers/Tomcat6.0/webapps/rentCar/WEB-INF/classes/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: JPA PersistenceProvider returned null EntityManagerFactory - check your JPA provider setup!
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.initializeBean(Abstract AutowireCapableBeanFactory.java:1455)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 94)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:225)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:291 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:567)
    at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:464)
    at org.springframework.web.context.ContextLoader.conf igureAndRefreshWebApplicationContext(ContextLoader .java:385)
    at org.springframework.web.context.ContextLoader.init WebApplicationContext(ContextLoader.java:284)
    at org.springframework.web.context.ContextLoaderListe ner.contextInitialized(ContextLoaderListener.java: 111)
    at org.apache.catalina.core.StandardContext.listenerS tart(StandardContext.java:4135)
    at org.apache.catalina.core.StandardContext.start(Sta ndardContext.java:4630)
    at org.apache.catalina.core.ContainerBase.addChildInt ernal(ContainerBase.java:791)
    at org.apache.catalina.core.ContainerBase.addChild(Co ntainerBase.java:771)
    at org.apache.catalina.core.StandardHost.addChild(Sta ndardHost.java:546)
    at org.apache.catalina.startup.HostConfig.deployDirec tory(HostConfig.java:1041)
    at org.apache.catalina.startup.HostConfig.deployDirec tories(HostConfig.java:964)
    at org.apache.catalina.startup.HostConfig.deployApps( HostConfig.java:502)
    at org.apache.catalina.startup.HostConfig.start(HostC onfig.java:1277)
    at org.apache.catalina.startup.HostConfig.lifecycleEv ent(HostConfig.java:321)
    at org.apache.catalina.util.LifecycleSupport.fireLife cycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1053)
    at org.apache.catalina.core.StandardHost.start(Standa rdHost.java:785)
    at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1045)
    at org.apache.catalina.core.StandardEngine.start(Stan dardEngine.java:445)
    at org.apache.catalina.core.StandardService.start(Sta ndardService.java:519)
    at org.apache.catalina.core.StandardServer.start(Stan dardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalin a.java:581)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootst rap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstr ap.java:414)
    Caused by: java.lang.IllegalStateException: JPA PersistenceProvider returned null EntityManagerFactory - check your JPA provider setup!
    at org.springframework.orm.jpa.AbstractEntityManagerF actoryBean.afterPropertiesSet(AbstractEntityManage rFactoryBean.java:312)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.invokeInitMethods(Abstr actAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.initializeBean(Abstract AutowireCapableBeanFactory.java:1452)
    ... 36 more

    Thanks
    Last edited by ltai701; Mar 10th, 2012, 09:45 AM. Reason: append my exception info

  • #2
    Hi, you can see an example to integrate spring and jpa here (http://www.apress.com/downloadable/d...sample_id/556/).

    ...\dataaccess\src\main\java\com\apress\springente rpriserecipes\course\jpa

    Comment

    Working...
    X