gpt4 book ai didi

How to Map Composite Primary key in JPA(如何在JPA中映射组合主键)

转载 作者:bug小助手 更新时间:2023-10-25 17:16:43 27 4
gpt4 key购买 nike



@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private long id;
@Column(name = "roll_no", nullable = false)
private long rollNo;
@Column(name = "enrollment_no", nullable = false, unique = true)
private String enrollmentNo;
}


@Entity
@Table(name = "subject")
public class Subject {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private long id;

@Column(name = "subject_code", nullable = false, unique = true)
private String code;// course code
}

I have the above two classes.
I want to create a StudentMarks class for below database table
student_marks(student_id, marks_id, marks, passing_status);

我上过上述两节课。我想为下面的数据库表Student_Marks(Student_id,Marks_id,Marks,Passing_Status)创建一个StudentMarks类;


I tried this

我试过这个


@Embeddable
public class StudentMarksId implements Serializable {

private Long studentId;
private Long subjected;
//getter and setter
}

@Entity
@Table(name = "student_marks")
public class StudentMarks implements Serializable {

private static final long serialVersionUID = 4428298147790767663L;

@EmbeddedId
@Id
@JoinColumns({
@JoinColumn(name = "subject_id", referencedColumnName = "id"),
@JoinColumn(name = "student_id", referencedColumnName = "id") })
private StudentMarksId id;


private List<Subject> subjects;
private List<Student> students;

@Column(name = "marks", nullable = false)
private int marks;

@Column(name = "passing_status", nullable = false)
private String passingStatus;// pass or fail
}```

I want to map student and subjects to student_marks.

更多回答

There are many tutorials on composite PKs, which have you been looking at? You cannot mark a single property (id) as EmbeddedId AND as an Id with join columns. Don't you need to have a single references to a Student and Subject in this entity somewhere? I don't see why you'd have them both as collections in there.

有很多关于复合PKS的教程,您看过哪些吗?不能将单个属性(Id)标记为EmbeddedID和具有联接列的ID。您不需要在此实体中的某个位置引用一个学生和主题吗?我不明白为什么你会把它们都作为收藏放在那里。

优秀答案推荐

There are a few ways to map this. One is:

有几种方法可以映射这一点。其一是:


public class StudentMarksId implements Serializable {
private Long studentId;
private Long subjected;
//getter and setter
}

@Entity
@Table(name = "student_marks")
@IdClass(StudentMarksId.class)
public class StudentMarks implements Serializable {
private static final long serialVersionUID = 4428298147790767663L;
@Id
@ManyToOne
@JoinColumn(name = "subject_id", referencedColumnName = "id")
private Subject subject;

@Id
@ManyToOne
@JoinColumn(name = "student_id", referencedColumnName = "id")
private Student student;

@Column(name = "marks", nullable = false)
private int marks;

@Column(name = "passing_status", nullable = false)
private String passingStatus;// pass or fail
}

Another might be to map the StudentMarksId as an embeddable in the entity as well:

另一种可能是将StudentMarksID映射为实体中的可嵌入对象:


@Embeddable
public class StudentMarksId implements Serializable {
private Long studentId;
private Long subjected;
//getter and setter
}

@Entity
@Table(name = "student_marks")
@IdClass(StudentMarksId.class)
public class StudentMarks implements Serializable {
private static final long serialVersionUID = 4428298147790767663L;

@EmbeddedId
private StudentMarksId id;

@MapsId("subjected")
@ManyToOne
@JoinColumn(name = "subject_id", referencedColumnName = "id")
private Subject subjects;

@MapsId("studentId")
@ManyToOne
@JoinColumn(name = "student_id", referencedColumnName = "id")
private Student student;

@Column(name = "marks", nullable = false)
private int marks;

@Column(name = "passing_status", nullable = false)
private String passingStatus;// pass or fail
}

更多回答

private Subject subjects; and private Student students; will be list and one to many annotation ?

民办科目;民办学生;会不会是一对多的注解?

No - This is essentially a relation table between Student and courses with the extra bit of information of a mark in the course. I've mapped them as a single instance, which defaults to OneToOne mapping since there is only one StudentMarks instance per Student-course pairing. You might be confused with a OneToMany with a ManyToOne back pointer. This StudentMarks.student relationship should be marked as the ManyToOne, as many StudentMarks point at the same Student (and many StudentMarks point at the same course). I'll update that in the answer.

否-这本质上是学生和课程之间的关系表,其中包含课程中标记的额外信息。我将它们映射为单个实例,默认为OneToOne映射,因为每个Student-course配对只有一个StudentMarks实例。您可能会将OneToMany与ManyToOne后指针混淆。这种StudentMarks.student关系应该标记为多对一,因为许多StudentMarks指向同一个Student(许多StudentMarks指向同一门课程)。我会在回答中更新这一点。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com