I currently scan packages for DAOs and services using Spring 3.1 & Hibernate 4 via <context:component-scan>
Is there a way to do the same for classes marked @Entity
instead of using the configLocation
property and a hbm.xml
file?
<hibernate-configuration> <session-factory> <mapping class="com.example.model.User" /> <!-- etc. --> </session-factory> </hibernate-configuration>
Basic entity mapping You just add an @Entity annotation to the class and an @Id annotation to the primary key attribute. Hibernate maps the entity to a database table with the same name and uses a default mapping for each attribute. You can define the same configuration with the following XML file.
HBM is a short name for Hibernate Mapping. It is an xml file in which we define the mapping between pojo class to database table and pojo class variables to table columns.
hbm. xml. The mapping document is an XML document having <hibernate-mapping> as the root element, which contains all the <class> elements. The <class> elements are used to define specific mappings from a Java classes to the database tables.
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" p:dataSource-ref="dataSource" p:configLocation="WEB-INF/classes/hibernate.cfg.xml" p:packagesToScan="com.example.model" />
Will scan everything in model package. I use my cfg.xml to contains settings like show_sql, and hb2ddl.auto.
You can do some like this in application context.xml file to scan all annotation classes -
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="lobHandler" ref="lobHandler"/> <property name="packagesToScan"> <list> <value>com.idc.scd.domain</value> <value>com.idc.scd.domain.dropdown</value> <value>com.idc.scd.domain.external</value> <value>com.idc.scd.domain.pk</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop> <prop key="hbm2ddl.auto">validate</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.connection.release_mode">after_statement</prop> <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop> <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop> <prop key="hibernate.cache.use_structured_entries">${hibernate.cache.use_structured_entries}</prop> <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop> </props> </property> </bean>
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