怎么读取hibernate的配置文件

2025-03-07 04:05:49
推荐回答(1个)
回答1:

hibernate从配置文件中读取和数据库连接有关的信息有两种格式,一种是XML文件配置,一种是属性文件配置即properties文件来配置,由于配置文件的不同导致使用hibernate api的时候会有微小差别,现在以hibernate2版本为例分别具体说明

1. 使用xml文件配置,文件格式一般为.cfg.xml,下面列举出了一个简要的配置

"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">





root
jdbc:mysql://localhost:3306/mydb
net.sf.hibernate.dialect.MySQLDialect
mydb
root
org.gjt.mm.mysql.Driver






其中connection.username制定用户名,connection.url制定数据库连接的url,dialect制定数据库方言,connection.password制定制定用户的密码,connection.driver_class制定驱动类,制定其中一个orm映射文件的位置,

下面列举出customer.hbm.xml的一个样例


"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >















同时需要建立数据库对应的java类文件

import java.io.Serializable;

public class Customer implements Serializable{
private Long id;
private String name;
private String email;
private String password;
private int phone;
private char sex;
private String address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}

使用hibernate api时具体示例代码如下

public class BusinessService {
public static SessionFactory sessionFactory;
static{
Configuration config = new Configuration();
try {
// config.addClass(Customer.class);
sessionFactory = config.configure().buildSessionFactory();

} catch (MappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public static void main(String[] args) {
Class c = Customer.class;
Customer customer = new Customer();
customer.setName("aaa");
customer.setPassword("1234");
customer.setEmail("alei_1304@163.com");
customer.setPhone(1234);
customer.setAddress("北京市中关村");
customer.setSex('M');
try {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(customer);
tx.commit();
session.close();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

其中采用xml文件配置,在初始化SessionFactory实例的时候需要采用new Configuration().configure().buildSessionFactory(),如果直接使用new Configuration().buildSessionFactory()会报异常java.lang.UnsupportedOperationException: The user must supply a JDBC connection

由于必须在xml配置文件中制定,所以在程序中无需再使用config.addClass(Customer.class),否则会导致异常net.sf.hibernate.MappingException: Error reading resource: com/beans/customer.hbm.xml
at net.sf.hibernate.cfg.Configuration.addResource(Configuration.java:340)
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:1027)
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:983)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:911)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:897)
at com.business.BusinessService.(BusinessService.java:17)
Caused by: net.sf.hibernate.MappingException: duplicate import: Customer

2.采用属性文件配置数据库连接的信息

需要配置一个属性文件来制定数据库连接信息,示例如下

hibernate.dialect = net.sf.hibernate.dialect.MYSQLDialect
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost:3306/mydb
hibernate.connection.username = root
hibernate.connection.password = root
hibernate.show_sql = true

orm映射文件和java类文件的配置同上,使用hibernate api示例代码如下

public class BusinessService {
public static SessionFactory sessionFactory;
static{
Configuration config = new Configuration();
try {
config.addClass(Customer.class);
sessionFactory = config.buildSessionFactory();

} catch (MappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public static void main(String[] args) {
Class c = Customer.class;
Customer customer = new Customer();
customer.setName("董磊");
customer.setPassword("1234");
customer.setEmail("alei_1304@163.com");
customer.setPhone(1234);
customer.setAddress("北京市中关村");
customer.setSex('M');
try {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(customer);
tx.commit();
session.close();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}