联合外键作主键 @EmbeddwdId

来源:互联网 发布:英语四级 知乎 编辑:程序博客网 时间:2024/05/17 02:42

嵌入类:

package com.dlnu.model;import java.io.Serializable;import javax.persistence.Embeddable;import javax.persistence.JoinColumn;import javax.persistence.ManyToOne;@Embeddablepublic class Name implements Serializable {private static final long serialVersionUID = 1L;private Course course;private Student student;public Name() {}public Name(Course course, Student student) {this.course = course;this.student = student;}@ManyToOne@JoinColumn(name = "courseId", referencedColumnName = "cno",nullable=true)public Course getCourse() {return course;}@ManyToOne@JoinColumn(name = "studentId", referencedColumnName = "sno")public Student getStudent() {return student;}public void setStudent(Student student) {this.student = student;}public void setCourse(Course course) {this.course = course;}public boolean equals(Object obj) {if (this == obj) {return true;}if (obj.getClass() == Name.class) {Name target = (Name) obj;if (target.getCourse().equals(course)&& target.getStudent().equals(student)) {return true;}}return false;}public int hashCode() {return course.hashCode() + student.hashCode() * 17;}}

 

实体类:

package com.dlnu.model;import javax.persistence.EmbeddedId;import javax.persistence.Entity;@Entitypublic class SC {private Name name;private Double grade;public Double getGrade() {return grade;}@EmbeddedIdpublic Name getName() {return name;}public void setGrade(Double grade) {this.grade = grade;}public void setName(Name name) {this.name = name;}}



 

原创粉丝点击