1、基于annotation的配置 many2one
student.java
package org.zttc.itat.model; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Table(name="t_student") public class Student { private int id; private String name; private String no; private Classroom classroom; @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; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="cid") public Classroom getClassroom() { return classroom; } public void setClassroom(Classroom classroom) { this.classroom = classroom; } }
classroom.java
package org.zttc.itat.model; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Table(name="t_student") public class Student { private int id; private String name; private String no; private Classroom classroom; @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; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="cid") public Classroom getClassroom() { return classroom; } public void setClassroom(Classroom classroom) { this.classroom = classroom; } }
在Hibernate.cfg.xml中需要以下的修改
<mapping class="com.baowei.entity.User" />
时间: 2024-10-11 23:19:08