现在的位置: 首页 > 综合 > 正文

联合外键作主键 @EmbeddwdId

2013年05月04日 ⁄ 综合 ⁄ 共 1324字 ⁄ 字号 评论关闭

嵌入类:

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;
	}

}

 

抱歉!评论已关闭.