我用annotation配置实体,用hibernate-cfg.xml配置的信息,但是在创建SessionFactory用AnnotationConfiguration的addXml()方法,怎么找不到文件呢?有没有annotation配置方面的例子
接着设置基本的Hibernate配置文件,可以使用XML或Properties档案,这边先使用XML,档名预设为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">
<!-- 显示实际操作数据库时的SQL -->
true
<!-- SQL方言,这边设定的是MySQL -->
org.hibernate.dialect.MySQLDialect
<!-- JDBC驱动程序 -->
com.mysql.jdbc.Driver
<!-- JDBC URL -->
jdbc:mysql://localhost/demo
<!-- 数据库使用者 -->
root
<!-- 数据库密码 -->
123456
<!-- 以下设置对象与数据库表格映像类别 -->
<mapping class="onlyfun.caterpillar.User"/>
</session-factory>
这边以一个简单的单机程序来示范Hibernate的配置与功能,首先作数据库的准备工作,在MySQL中新增一个demo数据库,并建立user表格:
CREATE TABLE user (
id INT(11) NOT NULL auto_increment PRIMARY KEY,
name VARCHAR(100) NOT NULL default '',
age INT
);
对于这个表格,您有一个User类别与之对应,表格中的每一个字段将对应至User实例上的Field成员。
package onlyfun.caterpillar;
import javax.persistence.*;
@Entity
@Table(name="user") // 非必要,在表格名称与类别名称不同时使用
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
@Column(name="name") // 非必要,在字段名称与属性名称不同时使用
private String name;
@Column(name="age")
private Integer age; // 非必要,在字段名称与属性名称不同时使用
// 必须要有一个预设的建构方法
// 以使得Hibernate可以使用Constructor.newInstance()建立对象
public User() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
其中id是个特殊的属性,Hibernate会使用它来作为主键识别,您可以定义主键产生的方式,这边设定为自动产生主键,可以看到,实体标识,主键生成,以及相关映像,都可以使用Annotation来完成。
接下来撰写一个测试的程序,这个程序直接以Java程序设计人员熟悉的语法方式来操作对象,而实际上也直接完成对数据库的操作,程序将会将一笔数据存入表格之中:
package onlyfun.caterpillar;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
public class HibernateAnnotationDemo {
public static void main(String[] args) {
// 需要AnnotationConfiguration读取Annotation讯息
Configuration config = new AnnotationConfiguration().configure();
// 根据 config 建立 SessionFactory
// SessionFactory 将用于建立 Session
SessionFactory sessionFactory = config.buildSessionFactory();
// 将持久化的物件
User user = new User();
user.setName("caterpillar");
user.setAge(new Integer(30));
// 开启Session,相当于开启JDBC的Connection
Session session = sessionFactory.openSession();
// Transaction表示一组会话操作
Transaction tx= session.beginTransaction();
// 将对象映像至数据库表格中储存
session.save(user);
tx.commit();
session.close();
sessionFactory.close();
System.out.println("新增资料OK!请先用MySQL观看结果!");
}
}
注意,使用Annotation时,需要的是AnnotationConfiguration类别。
http://www.iteye.com/topic/269295
不知道是你写错了还是原本你文件名就是这样::::hibernate-cfg.xml 应该是hibernate.cfg.xml
然后例子就是
SessionFactory sf = new AnnotationConfiguration.configure();
就可以得到了Session工厂了,不用你在配置的时候还要写入配置文件名,因为Hibernate已经默认了配置文件明,如果你的配置文件明是正确地就不用在写文件名了,希望能给你解决问题!还有不懂得追问吧!
不知道是你写错了还是原本你文件名就是这样::::hibernate-cfg.xml 应该是hibernate.cfg.xml
然后例子就是
SessionFactory sf = new AnnotationConfiguration.configure();
就可以得到了Session工厂了,不用你在配置的时候还要写入配置文件名,因为Hibernate已经默认了配置文件明,如果你的配置文件明是正确地就不用在写文件名了,希望能给你解决问题!还有不懂得追问吧!