класс отображения spring+hibernate без xml

Вместо того, чтобы определить пи как константу, я всегда использую acos(-1).

10
задан Pascal Thivent 30 April 2010 в 19:39
поделиться

4 ответа

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.

24
ответ дан 3 December 2019 в 13:25
поделиться

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.

5
ответ дан 3 December 2019 в 13:25
поделиться

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

3
ответ дан 3 December 2019 в 13:25
поделиться

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 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>
17
ответ дан 3 December 2019 в 13:25
поделиться
Другие вопросы по тегам:

Похожие вопросы: