联合外键作主键 @EmbeddwdId

嵌入类:

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

}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值