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

Pra*_*eek 8 java hibernate

我已经粘贴了Hibernate 3配置文件,SessionFactory类来配置这个config.xml和一个带有JPA注释的bean.我想知道我是否正在使用Hibernate 4然后在代码级别的上下文中的变化或非常广泛的差异或外行语言的进步.

的hibernate.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>
Run Code Online (Sandbox Code Playgroud)

静态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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Bean类

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

Mik*_*unu 6

的hibernate.cfg.xml

文件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(vs. ..sourceforge ...),但两者都应该有效.

建设会议工厂

正如您在API中看到的那样,不推荐使用buildSessionFactory.这是它在4.x中的构建方式:

Configuration conf = new Configuration();
conf.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(conf.getProperties()).buildServiceRegistry();        
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Run Code Online (Sandbox Code Playgroud)

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

注释实体

通常,bean类中的映射不需要进行任何更改.原因是您正在使用普通的JPA映射,而Hibernate 3也是JPA规范中描述的实现.