views:

556

answers:

4

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>
+6  A: 

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>
Pascal Thivent
+2  A: 

You can use annotations on your class, though for Hibernate I'm not sure there's something built in (for use in Spring). This thread should help if yo want to avoid yet more technology, but you could also use Java Persistence Annotations (JPA) in concert with Hibernate to accomplish the same goal.

Here's a good tutorial for using JPA + Hibernate + Spring.

Mark E
+1  A: 

@Pascal Thivent gives a very good start to what you want to do. For each Entity class you will need to annotate them. This is the docs that expalin the annotations in Hibernate.

e.g. From the docs:

   @Entity
  public class Flight implements Serializable {
    Long id;

    @Id
    public Long getId() { return id; }

    public void setId(Long id) { this.id = id; }
  }

So in addition to what was mentioned by @Pascal Thivent you should get everything you need.

Vincent Ramdhanie
+2  A: 

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.

non sequitor