[英]Discover JPA annotated classes when using Spring 3+Hibernate JPA
我有一个使用Spring 3 + Hibernate JPA堆栈的Web应用程序。
我想知道是否有办法让Hibernate自动发现@Entity带注释的类,这样我就不必在persistence.xml文件中列出它们。 我的@Entity注释类在一个单独的jar中“活动”,位于我的Web应用程序的WEB-INF / lib中。
这是我的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"/>
您可以将persistence.xml文件放在实体所在的jar中。 不需要指定任何东西,它可以自动运行。
你也可以像这样在applicationContext.xml中指定你的@Entity注释类
<property name="packagesToScan">
<list>
<value>com.vattikutiirf.nucleus.domain</value>
<value>com.vattikutiirf.nucleus.domain.generated.secondtime</value>
</list>
</property>
使用persistence.xml
<jar-file>
元素指定要扫描实体的单独jar文件。 因此,如果您的实体位于/WEB-INF/lib/entities.jar
,则需要
<jar-file>lib/entities.jar</jar-file>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.