我很确定我过去在JPA 2.0中使用了@Entity注释的bean的某种自动检测,但我无法弄清楚如何。你如何做而不是将每个bean列在persistence.xml中的class
XML元素中?
答案 0 :(得分:22)
您需要在下一行添加persistence.xml
:
<exclude-unlisted-classes>false</exclude-unlisted-classes>
e.g。
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" ...>
<persistence-unit name="YourPU" ...>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.level" value="ALL"/>
<property name="eclipselink.ddl-generation"
value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>
答案 1 :(得分:9)
从Spring 3.1开始,您还可以选择forget persistence.xml,并使用EntityManagerFactory
属性配置packagesToScan
,类似于:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"
p:packagesToScan="${jpa.entity.packages}">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:showSql="${hibernate.show_sql}"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
答案 2 :(得分:0)
请参阅Pascal Thivent答案:Do I need <class> elements in persistence.xml?
你有不同的方法,但JPA本身不支持自动扫描。引用您的实体的最简单,最干净的方法是将您的模型打包到jar中并使用<jar-file>MyModel.jar</jar-file>