Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring+hibernate mapping class without xml

in my applicationContext.xml, this is how I map xml to POJO. how to map directory to class file without required to create xml?

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
            <list>
                <value>com/custompackage/custom/spi/hibernate3/HibernateCurrentStep.hbm.xml</value>
                <value>com/custompackage/custom/spi/hibernate3/HibernateHistoryStep.hbm.xml</value>
                 <value>com/custompackage/custom/spi/hibernate3/HibernatecustomEntry.hbm.xml</value>
                  <value>user/custom/hibernate3/PropertySetItemImpl.hbm.xml</value>
                   <value>com/custompackage/user/provider/hibernate3/user/impl/HibernateGroupImpl.hbm.xml</value>
                   <value>com/custompackage/user/provider/hibernate3/user/impl/HibernateUserImpl.hbm.xml</value>
            </list>
        </property>


        <property name="hibernateProperties">
            .....
        </property>
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>

    </bean>
like image 758
cometta Avatar asked Oct 31 '09 14:10

cometta


2 Answers

And you can further simplify things by converting

<property name="annotatedClasses">
     <list>
       <value>com.mycompany.sample.domain.Order</value>
       <value>com.mycompany.sample.domain.LineItem</value>
       ...
     </list>
 </property>

to

<property name="packagesToScan" value="com.mycompany.sample.domain" />

in your AnnotationSessionFactoryBean so now all classes annotated with @Entity in the com.mycompany.sample.domain package will be automatically picked up.

like image 167
non sequitor Avatar answered Oct 11 '22 02:10

non sequitor


Instead of using XML mapping files, you can use the Hibernate Annotations library which is based on Java 5 annotations.

As usual, you'll need to declare your persistence classes in the Hibernate configuration file (typically hibernate.cfg.xml), though you use the <mapping> element to declare your persistent classes:

    <hibernate-configuration>
      <session-factory>
        <mapping class="com.mycompany.sample.domain.Order"/>
        <mapping class="com.mycompany.sample.domain.LineItem"/>
      </session-factory>
    </hibernate-configuration>

If you are using the Spring framework, you can set up an annotation-based Hibernate session factory using the AnnotationSessionFactoryBean class, as shown here:

  <!-- Hibernate session factory -->
  <bean id="sessionFactory" 
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
   <property name="dataSource">
     <ref bean="dataSource"/>
   </property>
   <property name="hibernateProperties">
     <props>
       <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
       <prop key="hibernate.hbm2ddl.auto">create</prop>
       ...
     </props>
   </property>
   <property name="annotatedClasses">
     <list>
       <value>com.mycompany.sample.domain.Order</value>
       <value>com.mycompany.sample.domain.LineItem</value>
       ...
     </list>
   </property>
 </bean>
like image 25
Pascal Thivent Avatar answered Oct 11 '22 03:10

Pascal Thivent