Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discover 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 to have Hibernate to automatically discover @Entity annotated classes, so that I don't have to list them in the persistence.xml file. My @Entity annotated classes "live" in a separate jar, 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"/>
like image 324
Luciano Fiandesio Avatar asked Dec 17 '22 17:12

Luciano Fiandesio


2 Answers

You can put the persistence.xml file inside the jar where your entities live. Don't need to specify anything then, it works automagically.

like image 147
Qwerky Avatar answered Jan 11 '23 23:01

Qwerky


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

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

like image 40
jnivasreddy Avatar answered Jan 11 '23 22:01

jnivasreddy