Spring + Hibernate + JPA [英] Spring + Hibernate + JPA
问题描述
我一直在阅读关于这个问题的许多文档和教程,我一直在困惑我是否需要一个persistence.xml文件。另外,我也一直对如何设置我的applicationContext.xml文件感到困惑。
有人知道使用EntityManager学习Spring + Hibernate + JPA +的好网站吗?
我刚刚花了几周的时间尝试设置相同类型的项目。
您需要一个persistence.xml文件,并且它属于META-INF
以下是一个示例我的spring bean文件用于持久化:
< 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
xmlns:context =http://www.springframework.org/schema/context
xsi:schemaLocation =http://www.springframework.org/schema/beans
http http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context -3.0.xsd>
< context:property-placeholder location =/ WEB-INF / config.properties/>
< tx:annotation-driven />
< bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>
< bean id =jpaTemplateclass =org.springframework.orm.jpa.JpaTemplate>
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>
< bean id =dataSourceclass =org.springframework.jdbc.datasource.DriverManagerDataSource>
< property name =driverClassNamevalue =$ {db.driver}/>
< property name =urlvalue =$ {db.url}/>
< property name =usernamevalue =$ {db.user}/>
< property name =passwordvalue =$ {db.password}/>
< / bean>
< bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =persistenceUnitNamevalue =whatisayis/>
< property name =dataSourceref =dataSource/>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter>
< property name =databasePlatformvalue =org.hibernate.dialect.MySQL5InnoDBDialect/>
< property name =showSqlvalue =true/>
< property name =generateDdlvalue =true/>
< / bean>
< / property>
< / bean>
< bean id =leDAOclass =com.noisyair.whatisayis.dao.jpa.JpaLearningEntryDAO>
< property name =jpaTemplateref =jpaTemplate/>
< / bean>
< bean id =sampleDAOclass =com.noisyair.whatisayis.dao.jpa.JpaSampleDAO>
< property name =jpaTemplateref =jpaTemplate/>
< / bean>
< bean id =tagDAOclass =com.noisyair.whatisayis.dao.jpa.JpaTagDAO>
< property name =jpaTemplateref =jpaTemplate/>
< / bean>
< / beans>
另外,我正在使用Maven来拉入我需要的spring3和hibernate依赖项。
编辑:对于学习资源我强烈建议Spring Recipes A Problem-Solution Approach由Gary Mac http://www.apress.com/book/view/9781590599792 。这是我读过的最好的技术书籍之一,它肯定会帮助你启动并运行Spring / JPA / Hibernate。
As of now I have a working Spring application with persistence. However now I want to use Hibernate with JPA to do all of my database activities. I want to do this using an entitymanager.
I've been reading many documents and tutorials on this matter, I've been getting confused on whether I need a persistence.xml file or not. Also I've been getting confused on how to setup my applicationContext.xml file as well.
Does anybody know of a good site to look at in order to learn Spring + Hibernate + JPA + using EntityManager?
I've just spent the last couple of weeks trying to set up the same kind of project.
You do need a persistence.xml file, and it belongs in META-INF
Here is an example of my spring beans file for persistence:
<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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder location="/WEB-INF/config.properties" />
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.user}" />
<property name="password" value="${db.password}" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="whatisayis" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
</bean>
</property>
</bean>
<bean id="leDAO" class="com.noisyair.whatisayis.dao.jpa.JpaLearningEntryDAO">
<property name="jpaTemplate" ref="jpaTemplate" />
</bean>
<bean id="sampleDAO" class="com.noisyair.whatisayis.dao.jpa.JpaSampleDAO">
<property name="jpaTemplate" ref="jpaTemplate" />
</bean>
<bean id="tagDAO" class="com.noisyair.whatisayis.dao.jpa.JpaTagDAO">
<property name="jpaTemplate" ref="jpaTemplate" />
</bean>
</beans>
Also, I am using Maven to pull in the spring3 and hibernate dependencies i need.
edit: for a learning resource I highly recommend "Spring Recipes A Problem-Solution Approach" by Gary Mac http://www.apress.com/book/view/9781590599792. This is one of the best technical books I've ever read, and it will surely help you get up and running with Spring/JPA/Hibernate.
这篇关于Spring + Hibernate + JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!