Java 没有xml的spring+hibernate映射类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1654467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:42:24  来源:igfitidea点击:

spring+hibernate mapping class without xml

javahibernatespringorm

提问by cometta

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

在我的 applicationContext.xml 中,这就是我将 xml 映射到 POJO 的方式。如何在不需要创建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>

采纳答案by non sequitor

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 AnnotationSessionFactoryBeanso now all classes annotated with @Entityin the com.mycompany.sample.domainpackage will be automatically picked up.

在你的AnnotationSessionFactoryBean所以现在所有@Entitycom.mycompany.sample.domain包中注释的类都将被自动选择。

回答by Pascal Thivent

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

您可以使用基于 Java 5 注释的Hibernate Annotations库,而不是使用 XML 映射文件。

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 配置文件(通常是hibernate.cfg.xml<mapping>中声明您的持久性类,尽管您使用元素来声明您的持久性类:

    <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 AnnotationSessionFactoryBeanclass, as shown here:

如果您使用的是 Spring 框架,则可以使用AnnotationSessionFactoryBean该类设置基于注解的 Hibernate 会话工厂,如下所示:

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

回答by Mark Elliot

You can use annotations on your class, though for Hibernate I'm not sure there's something built in (for use in Spring). This threadshould 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.

你可以在你的类上使用注释,但对于 Hibernate,我不确定是否有内置的东西(在 Spring 中使用)。如果您想避免使用更多技术,此线程应该会有所帮助,但您也可以将 Java 持久性注释 (JPA) 与 Hibernate 结合使用来实现相同的目标。

Here'sa good tutorial for using JPA + Hibernate + Spring.

这是一个很好的使用 JPA + Hibernate + Spring 的教程。

回答by Vincent Ramdhanie

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

@Pascal Thivent 为您想要做的事情提供了一个很好的开始。对于每个实体类,您都需要对其进行注释。这是解释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.

因此,除了@Pascal Thivent 提到的内容之外,您还应该获得所需的一切。