如何在JPA 2.0中自动检测实体

Luc*_*uke 11 java spring jpa java-ee

我很确定我过去在JPA 2.0中使用了@Entity注释的bean的某种自动检测,但我无法弄清楚如何.你如何做而不是将每个bean列在classpersistence.xml 中的XML元素中?

Pau*_*gas 22

您需要添加到persistence.xml下一行:

<exclude-unlisted-classes>false</exclude-unlisted-classes>
Run Code Online (Sandbox Code Playgroud)

例如

<?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>
Run Code Online (Sandbox Code Playgroud)


zag*_*gyi 9

从Spring 3.1开始,您还可以选择完全忘记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>
Run Code Online (Sandbox Code Playgroud)

  • 我知道,但我问过JPA persistence.xml (4认同)