怎么读取hibernate的配置文件

 我来答
于台烟baby
2017-02-11 · 超过64用户采纳过TA的回答
知道答主
回答量:80
采纳率:0%
帮助的人:60.3万
展开全部
hibernate从配置文件中读取和数据库连接有关的信息有两种格式,一种是XML文件配置,一种是属性文件配置即properties文件来配置,由于配置文件的不同导致使用hibernate api的时候会有微小差别,现在以hibernate2版本为例分别具体说明

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

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

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">jdbc:mysql://localhost:3306/mydb</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="myeclipse.connection.profile">mydb</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>

<mapping resource="com/beans/customer.hbm.xml"/>
</session-factory>

</hibernate-configuration>

其中connection.username制定用户名,connection.url制定数据库连接的url,dialect制定数据库方言,connection.password制定制定用户的密码,connection.driver_class制定驱动类,<mapping resource="com/beans/customer.hbm.xml"/>制定其中一个orm映射文件的位置,

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

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>
<class name = "com.beans.Customer" table="customers" >
<id name = "id" column="id" type = "long">
<generator class="increment"/>
</id>
<property name="name" column = "name" type = "string" not-null="true" />
<property name="email" column = "email" type = "string" not-null="true" />
<property name="password" column = "password" type = "string" not-null="true" />
<property name="phone" column = "phone" type = "int" />
<property name="address" column = "address" type = "string" />
<property name="sex" column = "sex" type = "character" />
</class>
</hibernate-mapping>

同时需要建立数据库对应的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配置文件中制定<mapping resource="com/beans/customer.hbm.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.<clinit>(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();
}

}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式