hibernate3.0版本后支持annotation
O/R Mapping: Object Relationship Mapping
创建实体类
@Entity
public class Teacher {
private int id;
private String name;
private String title;
@Id // 主键
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
修改hibernate.cfg.xml
添加:
<mapping class="model.Teacher"/>
测试
public class TestTeacher {
public static void main(String[] args) {
Teacher t = new Teacher();
t.setId(1);
t.setName("t1");
t.setTitle("中级");
// Configuration cfg = new Configuration();
Configuration cfg = new AnnotationConfiguration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session sessions = sf.openSession();
sessions.beginTransaction();
sessions.save(t);
sessions.getTransaction().commit();
sf.close();
}
}
本文介绍Hibernate 3.0版本后引入的注解支持,并通过创建Teacher实体类演示如何使用@Entity和@Id等注解进行对象关系映射。此外,还展示了如何配置并保存Teacher实例。
599

被折叠的 条评论
为什么被折叠?



