API或代码:Hibernate 3和4之间的区别? [英] API or code : Difference between Hibernate 3 and 4?

查看:86
本文介绍了API或代码:Hibernate 3和4之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我粘贴了 Hibernate 3 配置文件,SessionFactory类用于配置此config.xml和一个包含JPA注释的bean。我想知道,如果我使用的是 Hibernate 4 ,那么代码级别上下文中的变化或者非常宽泛的外行语言差异或进步会是什么样的。


hibernate.cfg.xml



 < ;?xml version =1.0encoding =UTF-8?> 
<!DOCTYPE hibernate-configuration PUBLIC
- // Hibernate / Hibernate配置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>建立< / property>
< mapping class =com.vaannila.domain.User1/>
< / session-factory>
< / hibernate-configuration>




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




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

public class SessionFactoryHelper {
private static final SessionFactory sessionFactory;

static {
try {
/ *
*从hibernate.cfg.xml文件中定义的会话工厂配置
*构建SessionFactory对象。在这个文件中,我们注册了
*我们使用的JDBC连接信息,连接池,hibernate
*方言以及映射到每个
* POJO的hbm.xml文件(Plain Old Java目的)。
*
* /
sessionFactory = new Configuration()。configure()。buildSessionFactory();
} catch(Throwable e){
System.err.println(创建SessionFactory对象时出错。
+ e.getMessage());
抛出新的ExceptionInInitializerError(e);


$ b $ * b $ b *其他应用程序获得SessionFactory对象
*的一个静态方法,在此辅助类中初始化。
*
* /
public static SessionFactory getSessionFactory(){
return sessionFactory;




$ blockquote

Bean类 p>



  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 aboutYou;
私人布尔mailingList;

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

$ b $ @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;

$ b $ @Column(name =USER_MAILING_LIST)
public Boolean getMailingList(){
returns mailingList;
}
public void setMailingList(Boolean mailingList){
this.mailingList = mailingList;
}

}


解决方案

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

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



注解实体



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


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

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>

Static java class to establish connection (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;
    }

}

解决方案

hibernate.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_sql instead 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.

Building session factory

As you see from the API, buildSessionFactory is deprecated. This is how it is built in 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.

这篇关于API或代码:Hibernate 3和4之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆