Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scan packages for Hibernate entities instead of using hbm.xml?

Tags:

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> 
like image 878
blank Avatar asked Jun 29 '12 07:06

blank


People also ask

How will you map the entity in hibernate configuration file?

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.

What is the use of HBM file in hibernate?

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.

What are HBM XML files?

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.


2 Answers

<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.

like image 140
NimChimpsky Avatar answered Oct 04 '22 17:10

NimChimpsky


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> 
like image 25
Pramod Kumar Avatar answered Oct 04 '22 16:10

Pramod Kumar