java - Spring + hibernate + JPA

标签 java spring hibernate jpa entitymanager

到目前为止,我有一个具有持久性的 Spring 应用程序。但是现在我想使用 Hibernate 和 JPA 来完成我所有的数据库 Activity 。我想使用实体管理器来做到这一点。

我已经阅读了很多关于这个问题的文档和教程,我一直对是否需要persistence.xml 文件感到困惑。我也对如何设置我的 applicationContext.xml 文件感到困惑。

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

最佳答案

过去几周我刚刚尝试建立相同类型的项目。

你确实需要一个 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://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。

关于java - Spring + hibernate + JPA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2061410/

相关文章:

java - 我在使用 readStream().format ("kafka"时遇到问题)

java - 从内部 jar 加载脚本文件

java - 如何在 Spring JDBC 中创建对象之间的关系?

java - Hibernate 事件监听器 - 回滚

java - 执行的字符串值与硬编码值不匹配

java - android更改标签图标

java - 选择数据结构以根据用户评分从无数项中排序前 10 项

spring - 如何从 Spring 授权服务器示例中获取刷新 token

java - count() 方法之间的冲突 - CrudRepository - Spring

java - Servlet 抛出 java.lang.NullPointerException