日期:2014-05-19 浏览次数:20735 次
//给个实例吧,不过这个讲就涉及到很多细节了,建议你google,或者看视频,有些代码省略 @Entity @Table(name="t_student") public class Student { private int id; private String name; private Teacher teacher; @Id @GeneratedValue //设置主键,并且自动增长 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; } @ManyToOne @JoinColumn(name="teacherId") public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } } //Teacher.java 这里本来不是一对多的关系,为了写实例方便,表示一个学生只有拥有一个老师 @Entity @Table(name="t_teacher") public class Teacher { private int id; private String name; @Id @GeneratedValue 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; } } //import省略,即使我写上,你也不能直接运行,要导jar包的 //如果是只用到hibernate,没用到spring的话,还要单独一个hibernate的配置文件,祝你好运。。。