简体   繁体   English

使用Spring 3 + Hibernate JPA时,发现JPA带注释的类

[英]Discover JPA annotated classes when using Spring 3+Hibernate JPA

I have a web application using the Spring 3 + Hibernate JPA stack. 我有一个使用Spring 3 + Hibernate JPA堆栈的Web应用程序。

I would like to know if there is a way to have Hibernate to automatically discover @Entity annotated classes, so that I don't have to list them in the persistence.xml file. 我想知道是否有办法让Hibernate自动发现@Entity带注释的类,这样我就不必在persistence.xml文件中列出它们。 My @Entity annotated classes "live" in a separate jar, located in the WEB-INF/lib of my web application. 我的@Entity注释类在一个单独的jar中“活动”,位于我的Web应用程序的WEB-INF / lib中。

This is a snippet from my Spring configuration file: 这是我的Spring配置文件的片段:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
   <property name="persistenceUnitName" value="mypersistence"/>
   <property name="dataSource" ref="dataSource"/>
   <property name="jpaVendorAdapter">
       <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
           <property name="showSql" value="true"/>
           <property name="generateDdl" value="true"/>
           <property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect"/>
       </bean>
   </property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
    <property name="url" value="jdbc:derby://localhost:1527/library;create=true"/>
    <property name="username" value="app"/>
    <property name="password" value="app"/>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<bean id="persistenceAnnotation" class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

You can put the persistence.xml file inside the jar where your entities live. 您可以将persistence.xml文件放在实体所在的jar中。 Don't need to specify anything then, it works automagically. 不需要指定任何东西,它可以自动运行。

you can also specify your @Entity annotated classes in applicationContext.xml like this 你也可以像这样在applicationContext.xml中指定你的@Entity注释类

<property name="packagesToScan">
     <list>
        <value>com.vattikutiirf.nucleus.domain</value>
        <value>com.vattikutiirf.nucleus.domain.generated.secondtime</value>
     </list>
  </property>

The separate jar files to scan for entities are specified using <jar-file> elements in persistence.xml . 使用persistence.xml <jar-file>元素指定要扫描实体的单独jar文件。 So, if you entities are located in /WEB-INF/lib/entities.jar , you need 因此,如果您的实体位于/WEB-INF/lib/entities.jar ,则需要

<jar-file>lib/entities.jar</jar-file>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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