Open JPA annotated classes when using Spring 3 + Hibernate JPA

I have a web application using the Spring 3 + Hibernate JPA stack.

I would like to know if there is a way for Hibernate to automatically detect annotated @Entity classes, so I don't need to list them in the persistence.xml file. My @Entity annotated the "live" classes in a separate bank located in the WEB-INF / lib of my web application.

This is a snippet from my Spring configuration file:

<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"/> 
+4
source share
3 answers

You can put the persistence.xml file in the jar where your objects live. No need to specify anything, it works automatically.

+7
source

you can also specify your annotated @Entity classes in applicationContext.xml as

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

+3
source

Separate jar files for scanning objects are specified using <jar-file> elements in persistence.xml . So, if the objects are in /WEB-INF/lib/entities.jar , you need to

 <jar-file>lib/entities.jar</jar-file> 
+2
source

Source: https://habr.com/ru/post/1338067/


All Articles