spring+hibernate mapping class without xml

前端 未结 4 1154
不知归路
不知归路 2021-02-03 10:58

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



        
相关标签:
4条回答
  • 2021-02-03 11:54

    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.

    0 讨论(0)
  • 2021-02-03 11:57

    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.

    0 讨论(0)
  • 2021-02-03 11:59

    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>
    
    0 讨论(0)
  • 2021-02-03 11:59

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

    0 讨论(0)
提交回复
热议问题