Java API或代码:Hibernate3和4之间的区别?
我粘贴了Hibernate 3配置文件、SessionFactory类来配置这个config.xml和一个带有JPA注释的bean。我想知道如果我使用的是Hibernate4,那么在代码级别的上下文中会有什么变化,或者在外行语言中会有什么非常广泛的差异或进步 hibernate.cfg.xml 豆类 hibernate.cfg.xml 文件hibernate.cfg.xml很好。hibernate-configuration-3.0.dtd中的版本仍然是3.0,这看起来可能令人困惑,但事实就是如此。DTD未更新。也许您想使用以hibernate为前缀的名称,例如Java API或代码:Hibernate3和4之间的区别?,java,hibernate,Java,Hibernate,我粘贴了Hibernate 3配置文件、SessionFactory类来配置这个config.xml和一个带有JPA注释的bean。我想知道如果我使用的是Hibernate4,那么在代码级别的上下文中会有什么变化,或者在外行语言中会有什么非常广泛的差异或进步 hibernate.cfg.xml 豆类 hibernate.cfg.xml 文件hibernate.cfg.xml很好。hibernate-configuration-3.0.dtd中的版本仍然是3.0,这看起来可能令人困惑,但事实就是如
hibernate.show_sql
而不是show_sql
。属性的名称可以从中找到。通常使用的DTD\U位置是http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd
(vs…sourceforge…),但两者都应该有效
建筑会话工厂
正如您从中看到的,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规范中描述的实现。请不要投反对票。这是一个重要的问题,我将要求在我的项目中进行技术更改。不评论背后的原因就投反对票是没有建设性的
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;
}
}
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;
}
}
Configuration conf = new Configuration();
conf.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(conf.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);