首页 > 代码库 > hibernate 一对一,一对多
hibernate 一对一,一对多
package com.web.user.bean;
import java.io.Serializable;
import java.sql.Timestamp;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.Range;
import com.web.teacher.action.Academy;
@Entity
@Table(name="teacher")
public class Teacher implements Serializable{
private static final long serialVersionUID = 7975734970176620572L;
private Long id; //老师编号
@NotBlank(message="用户名不能为空")
@Length(min=6,max=20,message="用户名长度必须在6-20之间")
private String username; //老师用户名
@NotBlank(message="密码不能为空")
@Length(min=6,max=20,message="密码长度必须在6-20之间")
private String password; //密码
private String name; //姓名
private String sex; //性别
private String telphone; //联系方式
private String photoPath; //照片路径
@NotNull(message="审批资格不为空")
@Range(min=1,max=2,message="操作序号必须为1-2的整数")
private Short titleKey; //是否获得审批资格 1表示未获得审批人员资格失败,2表示获得审批员资格成功
private String ip; //审批人登陆IP
private Timestamp loginTime;//审批人登陆时间
private Timestamp registerTime;//申请时间
private Academy academy; //所属学院
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Column
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column
public String getTelphone() {
return telphone;
}
public void setTelphone(String telphone) {
this.telphone = telphone;
}
@Column
public String getPhotoPath() {
return photoPath;
}
public void setPhotoPath(String photoPath) {
this.photoPath = photoPath;
}
@Column
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
@Column
public Timestamp getLoginTime() {
return loginTime;
}
public void setLoginTime(Timestamp loginTime) {
this.loginTime = loginTime;
}
@Column
public Short getTitleKey() {
return titleKey;
}
public void setTitleKey(Short titleKey) {
this.titleKey = titleKey;
}
@Column
public Timestamp getRegisterTime() {
return registerTime;
}
public void setRegisterTime(Timestamp registerTime) {
this.registerTime = registerTime;
}
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="academyId")
public Academy getAcademy() {
return academy;
}
public void setAcademy(Academy academy) {
this.academy = academy;
}
}
package com.web.teacher.action;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.web.user.bean.Teacher;
@Entity
@Table(name="academy")
public class Academy implements Serializable{
private static final long serialVersionUID = 1L;
private Long academyId; //学院编号
private String name; //学院名称
private Set<Teacher> teachers = new HashSet<Teacher>(); //老师
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Long getAcademyId() {
return academyId;
}
public void setAcademyId(Long academyId) {
this.academyId = academyId;
}
@Column
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//
// @ManyToOne(cascade=CascadeType.ALL,mappedBy="acdAcademy")
// public Teacher getTeacher() {
// return teacher;
// }
//
// public void setTeacher(Teacher teacher) {
// this.teacher = teacher;
// }
@OneToMany(cascade = CascadeType.ALL,mappedBy="academy")
public Set<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(Set<Teacher> teachers) {
this.teachers = teachers;
}
}
public class TeacherAction {
@Autowired
private TeacherService service;
private File headfile; //上传文件
private String headfileFileName; //上传文件名
private Teacher teacher; //老师
private Academy academy; //学院
public Academy getAcademy() {
return academy;
}
public void setAcademy(Academy academy) {
this.academy = academy;
}
public File getHeadfile() {
return headfile;
}
public void setHeadfile(File headfile) {
this.headfile = headfile;
}
public String getHeadfileFileName() {
return headfileFileName;
}
public void setHeadfileFileName(String headfileFileName) {
this.headfileFileName = headfileFileName;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
/**
* 注册
* @return
*/
public String register(){
try {
upFile();
} catch (Exception e) {
System.out.println("上传失败或者没有上传文件");
}
Set<Teacher> teachers = new HashSet<Teacher>();
teachers.add(teacher);
academy.setTeachers(teachers);
teacher.setAcademy(academy);
boolean submit= service.register(teacher);
if(submit){
return "success";
}else{
return "failure";
}
}
hibernate 一对一,一对多