Spring + Hibernate + JPA

Alb*_*ish 28 java spring hibernate jpa entitymanager

到目前为止,我有一个具有持久性的Spring应用程序.但是现在我想使用Hibernate和JPA来完成我的所有数据库活动.我想使用entitymanager来做这件事.

我一直在阅读有关此问题的许多文档和教程,我一直对是否需要persistence.xml文件感到困惑.此外,我也对如何设置applicationContext.xml文件感到困惑.

有没有人知道一个好的网站,以便使用EntityManager学习Spring + Hibernate + JPA +?

D.C*_*.C. 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>
Run Code Online (Sandbox Code Playgroud)

另外,我正在使用Maven来引入我需要的spring3和hibernate依赖项.

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