views:

346

answers:

2

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?

+3  A: 

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"&gt;

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

darren
+1 for Spring Recipes, 1 of the best tech books ever.
Kaleb Brasee
thank you for that, it helped a lot
Albinoswordfish
A: 

Hi there, the following tutorial might be helpful:

http://javacodegeeks.blogspot.com/2010/05/jboss-42x-spring-3-jpa-hibernate.html