Spring + Hibernate + JPA

28
目前我有一个可以持久化的Spring应用程序,但现在我想使用Hibernate和JPA来执行所有的数据库活动。我希望使用EntityManager来进行操作。
我已经阅读了很多关于这个问题的文档和教程,但我对是否需要一个persistence.xml文件感到困惑。此外,我也不确定如何设置我的applicationContext.xml文件。
有没有人知道一个可以学习Spring + Hibernate + JPA + 使用EntityManager的好网站?

6
为什么这个问题被关闭了?“我一直在困惑如何设置我的applicationContext.xml文件。”- 那个问题难道不够清晰和具体吗?拜托,不要关闭那些有帮助的问题。 - Blessed Geek
4
我同意“Blessed Geek”的观点。这是一个非常受欢迎的问题,我也在寻找这个答案。不要关闭有用的问题。如果关闭该问题的是脚本,请改正代码。 - Combustion007
这里有一门在线课程,可以学习使用Maven管理依赖项的Spring Jpa和Hibernate:http://pluralsight.com/training/Courses/TableOfContents/spring-jpa-hibernate - bh5k
1个回答

12

我刚刚花了几周的时间尝试设置同样类型的项目。

你确实需要一个 persistence.xml 文件,并且它应该放在 META-INF 目录下。

以下是我用于持久化的 spring beans 文件示例:

<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>

此外,我正在使用Maven获取我所需的Spring3和Hibernate依赖项。

编辑:对于学习资源,我强烈推荐Gary Mac的《Spring Recipes A Problem-Solution Approach》http://www.apress.com/book/view/9781590599792。这是我读过的最好的技术书籍之一,它一定会帮助您开始并运行Spring/JPA/Hibernate。


+1 for Spring Recipes,这是最好的技术书籍之一。 - Kaleb Brasee
此外,是否有可能从配置文件中自动加载多个(1到n个不同的)数据源? - Vedran
从Spring 3.2开始,您不再需要额外的persistence.xml文件。 - bh5k

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接