java API 或代码:Hibernate 3 和 4 之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11499929/
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-10-31 05:20:50  来源:igfitidea点击:

API or code : Difference between Hibernate 3 and 4?

javahibernate

提问by Prateek

I have pasted Hibernate 3configuration file , SessionFactory class to configure this config.xml and a bean with JPA annotations. I want to know if I were using Hibernate 4then what would have been the changes in the context at code level or very broad differences or advancements in layman language.

我已经粘贴了Hibernate 3配置文件、SessionFactory 类来配置这个 config.xml 和一个带有 JPA 注释的 bean。我想知道如果我使用的是Hibernate 4,那么代码级别的上下文会发生什么变化,或者外行语言的非常广泛的差异或进步。

hibernate.cfg.xml

休眠文件.cfg.xml

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@192.168.2.144:1521:xe</property>
        <property name="hibernate.connection.username">prateek</property>
        <property name="connection.password">prateek</property>
        <property name="connection.pool_size">1</property>
        <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
        <mapping class="com.vaannila.domain.User1" />
    </session-factory>
</hibernate-configuration>

Static java class to establish connection (SessionFactory Helper)

建立连接的静态java类(SessionFactory Helper)

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class SessionFactoryHelper {
    private static final SessionFactory sessionFactory;

    static {
        try {            
            /*
             * Build a SessionFactory object from session-factory configuration 
             * defined in the hibernate.cfg.xml file. In this file we register 
             * the JDBC connection information, connection pool, the hibernate 
             * dialect that we used and the mapping to our hbm.xml file for each 
             * POJO (Plain Old Java Object).
             * 
             */
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable e) {
            System.err.println("Error in creating SessionFactory object." 
                + e.getMessage());
            throw new ExceptionInInitializerError(e);
        }
    }

    /*
     * A static method for other application to get SessionFactory object 
     * initialized in this helper class.
     * 
     */
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

Bean class

豆类

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="USER1")
public class User1 {

    private Long id;
    private String name;
    private String gender;
    private String country;
    private String aboutYou;
    private Boolean mailingList;

    @Id
    @GeneratedValue
    @Column(name="USER_ID") 
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    @Column(name="USER_NAME")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Column(name="USER_GENDER")
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }

    @Column(name="USER_COUNTRY")
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }

    @Column(name="USER_ABOUT_YOU")
    public String getAboutYou() {
        return aboutYou;
    }
    public void setAboutYou(String aboutYou) {
        this.aboutYou = aboutYou;
    }

    @Column(name="USER_MAILING_LIST")
    public Boolean getMailingList() {
        return mailingList;
    }
    public void setMailingList(Boolean mailingList) {
        this.mailingList = mailingList;
    }

}

采纳答案by Mikko Maunu

hibernate.cfg.xml

休眠文件.cfg.xml

File hibernate.cfg.xml is fine. It may look confusing that version is still 3.0 in hibernate-configuration-3.0.dtd, but thats how it is. DTD was not updated. Maybe you want to use names names prefixed with hibernate instead, for example hibernate.show_sqlinstead of show_sql. Names of properties can be found from documentation. Usually DTD_location used is http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd(vs. ..sourceforge...), but both should work.

文件 hibernate.cfg.xml 没问题。在 hibernate-configuration-3.0.dtd 中版本仍然是 3.0 可能看起来令人困惑,但事实就是这样。DTD 未更新。也许您想使用以 hibernate 为前缀的名称,例如hibernate.show_sql代替show_sql. 属性名称可以从文档中找到。通常使用的 DTD_location 是http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd(与 ..sourceforge ...),但两者都应该有效。

Building session factory

建立会话工厂

As you see from the API, buildSessionFactory is deprecated. This is how it is built in 4.x:

正如您从API 中看到的, buildSessionFactory 已被弃用。这是它在 4.x 中的构建方式:

Configuration conf = new Configuration();
conf.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(conf.getProperties()).buildServiceRegistry();        
sessionFactory = configuration.buildSessionFactory(serviceRegistry);

In many places in documentation this is still not up to date.

在文档的许多地方,这仍然不是最新的。

Annotating entities

注释实体

In general no changes are needed to your mappings in bean class. Reason is that you are using normal JPA mappings and also Hibernate 3 is implementation of what is described in JPA specification.

一般来说,您在 bean 类中的映射不需要更改。原因是您使用的是普通的 JPA 映射,而且 Hibernate 3 是 JPA 规范中描述的实现。