We have a web application based on Spring 3.2.4 and Hibernate 4.2.3. It was generated using the appfuse 2.2.1 archetype. As per the documentation available, we can use the packagesToScan property of the SessionFactory to dynamically pick up entity classes and set up the sessionFactory. However, we face an issue that we have to explicitly mention the entity class name in hibernate.cfg.xml for it to be recognized by hibernate. I could not find any appropriate answers on forums, so posting here. It may be that my understanding of how it works may be incorrect, so any pointers are welcome.
Thanks
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans -- namespace declarations here
default-lazy-init="true">
<!-- Activates scanning of @Autowired -->
<context:annotation-config/>
<!-- Activates scanning of @Repository and @Service -->
<context:component-scan base-package="com.xyz"/>
</beans>
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping class="com.xyz.model.Address" />
<mapping class="com.xyz.model.Company" />
<mapping class="com.xyz.model.CompanyBilling" />
<!-- BIG LIST OF MY ENTITY CLASSES -->
</session-factory>
</hibernate-configuration>
applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans -- namespace declarations here
default-lazy-init="true">
<bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator"/>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" destroy-method="destroy">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
</props>
</property>
<property name="packagesToScan"><list><value>com.xyz.model</value></list></property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- Activates scanning of @Autowired -->
<context:annotation-config/>
<!-- Activates scanning of @Repository -->
<context:component-scan base-package="com.xyz.dao"/>
</beans>
Company.java
package com.xyz.model;
// imports
@Entity
@Table(name = "company")
@SQLDelete(sql = "UPDATE company SET activeFlag = 0, lastupdated=now() WHERE id = ?")
@Where(clause = "1=activeflag")
@Component
public class Company extends BaseEntity implements java.io.Serializable {
// properties and getter setters
}
The second property packagesToScan specifies Java package to automatically scan for annotated entity classes. This way it is no longer necessary to prepare Hibernate mapping file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With