简体   繁体   中英

Configuring JPA in Spring application

I am configuring Spring to use JPA by using Hibernate implementation. However I don't understand the process completly. I have gotten it to work by following different blogs etc. I have used EJB 3.1 and there I had a persistence.xml. However in spring I declared a LocalContainer...Bean and provided some properties to it, and I have no persistence.xml. Could someone explain how it works in Spring and what the declared bean is?

<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="packagesToScan" value="com.company.domain" />
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">
                org.hibernate.dialect.PostgreSQL82Dialect
            </prop>
        </props>
    </property>
</bean>

There are different flavors of Spring Configuration with JPA, one which requires persistence.xml and other which requires just bean declarations(no persistence.xml).

I am going to take up the Case-2 in your scenario:

The main reasons we want a persistence.xml is because of the following reasons:

  1. Database connectivity details.
  2. Java classes which are treated as Entities or packages in which to scan for Entities.
  3. Other vendor specific settings like hibernate.show_sql or similar stuff.

Now if spring provides a way to mention all this together in bean configurations then there is no need to have the persistence.xml.

In case of your bean definitions, lets break it down.

<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="packagesToScan" value="com.company.domain" />
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">
                org.hibernate.dialect.PostgreSQL82Dialect
            </prop>
        </props>
    </property>
</bean>

First property, dataSource already contains the database settings.

Second property, jpaVendorAdapter is a property specific to Spring

Third property, packagesToScan this is a property of Spring to scan for entities, this we either do in persistence.xml by using "class" tags by mentioning each class FQN.

Fourth property, jpaProperties as the name suggests can either be in Spring or in persistence.xml

eg

<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect" />

Since you have all the configurations already in Spring bean, there's no need to have a persistence.xml

Just to add a FootNote:

Spring 3.1 provides an alternative: LocalContainerEntityManagerFactoryBean that accepts a 'packagesToScan' property, specifying base packages to scan for @Entity classes.

Hope this answer your queries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM