关闭

联合外键作主键 @EmbeddwdId

标签: equalsclassobject
1805人阅读 评论(0) 收藏 举报
分类:

嵌入类:

package com.dlnu.model;

import java.io.Serializable;

import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Embeddable
public 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;

@Entity
public class SC {

	
	private Name name;

	private Double grade;
	
	public Double getGrade() {
		return grade;
	}
	
	@EmbeddedId
	public Name getName() {
		return name;
	}
	public void setGrade(Double grade) {
		this.grade = grade;
	}
	public void setName(Name name) {
		this.name = name;
	}

}



 

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:90485次
    • 积分:1007
    • 等级:
    • 排名:千里之外
    • 原创:22篇
    • 转载:11篇
    • 译文:0篇
    • 评论:14条
    文章分类
    最新评论