在Spring 集成 Hibernate 的JPA方式中,需要在persistence配置文件中定义每一个实体类,这样非常地不方便,2种方法可以解决此问题:
这2种方式都可以实现不用在persistence.xml文件中配置每一个实体类,从而免去每个Entity都要在persistence.xml文件中配置的烦恼,但是这种方式Entity实体类的主键字段注解@ID要放到 getXXX()方法上,否则不认。
方式1:
修改“LocalContainerEntityManagerFactoryBean”的配置,如下:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="com.sunitjy.model.entityName" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.connection.driver_class">${jdbc.driverClassName}</prop>
<prop key="hibernate.connection.url">${jdbc.url}</prop>
<prop key="hibernate.connection.username">${jdbc.username}</prop>
<prop key="hibernate.connection.password">${jdbc.password}</prop>
<prop key="hibernate.c3p0.min_size">10</prop>
<prop key="hibernate.hbm2ddl.auto">true</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
</props>
</property>
</bean>
方式1没有使用 persistence 这个配置文件,注意咯!
方式2:
修改“LocalContainerEntityManagerFactoryBean”的配置,如下:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- <property name="persistenceUnitName" value="pro_persistence"></property>-->
<property name="dataSource" ref="dataSource"></property>
<property name="persistenceXmlLocation" value="classpath*:pro_core/jpa_persistence.xml"></property>
<property name="packagesToScan">
<list>
<value>com.paic.lfex.model</value>
<value>com.lfex.sdp.core.model</value>
<value>com.paic.lfex.core.pro</value>
</list>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
</bean>
persistence.xml配置文件内容: