gpt4 book ai didi

java - 在jpa中添加外键和复合主键?

转载 作者:太空宇宙 更新时间:2023-11-04 12:01:57 26 4
gpt4 key购买 nike

我的数据库中有 3 个表:教授、学科和类(class)。从教授、学科到类(class),这是多对一的关系。我尝试过输入外键,但不起作用。

类(class)类别:

package com.licenta.ascourses.model;

import java.io.Serializable;

import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="Courses")
@AssociationOverrides({
@AssociationOverride(name = "pk.discipline",
joinColumns = @JoinColumn(name = "IDDISCIPLINE")),
@AssociationOverride(name = "pk.professor",
joinColumns = @JoinColumn(name = "IDPROFESSOR")) })
public class Course implements Serializable {



private CourseId idCourse=new CourseId();
private int year;
private int semester;


public Course()
{


}


public Course(CourseId idCourse, int idDiscipline, int idProfessor,int year, int semester) {
super();
this.idCourse = idCourse;
this.year = year;
this.semester = semester;
}


@EmbeddedId
public CourseId getIdCourse() {
return idCourse;
}


public void setIdCourse(CourseId idCourse) {
this.idCourse = idCourse;
}


public int getYear() {
return year;
}


public void setYear(int year) {
this.year = year;
}


public int getSemester() {
return semester;
}


public void setSemester(int semester) {
this.semester = semester;
}


}

复合主键的 CourseId 类:

package com.licenta.ascourses.model;

import java.io.Serializable;

import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;


@Embeddable
public class CourseId implements Serializable{

@ManyToOne
@JoinColumn(name="IDDISCIPLINE")
private int idDiscipline;

@ManyToOne
@JoinColumn(name="IDPROFESSOR")
private int idProfessor;
private String courseNo;



public int getIdDiscipline() {
return idDiscipline;
}

public void setIdDiscipline(int idDiscipline) {
this.idDiscipline = idDiscipline;
}

public int getIdProfessor() {
return idProfessor;
}

public void setIdProfessor(int idProfessor) {
this.idProfessor = idProfessor;
}

public String getCourseNo() {
return courseNo;
}

public void setCourseNo(String courseNo) {
this.courseNo = courseNo;
}

public boolean equals(Object o) {

return true;
}

public int hashCode() {

return 1;
}


}

学科类别:

package com.licenta.ascourses.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;



@Entity
@Table(name="Disciplines")
public class Discipline implements Serializable {
private int idDiscipline;
@Id
private String name;
private String description;
private int an;
private int semestru;




public Discipline()
{

}

public Discipline(int idDiscipline, String name, String description, int an, int semestru) {
super();
this.idDiscipline = idDiscipline;
this.name = name;
this.description = description;
this.an = an;
this.semestru = semestru;
}




public Discipline(int idDiscipline, String name, String description, int an, int semestru, Set<Course> courses) {
super();
this.idDiscipline = idDiscipline;
this.name = name;
this.description = description;
this.an = an;
this.semestru = semestru;

}


public int getIdDiscipline() {
return idDiscipline;
}


public void setIdDiscipline(int idDiscipline) {
this.idDiscipline = idDiscipline;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public String getDescription() {
return description;
}


public void setDescription(String description) {
this.description = description;
}


public int getAn() {
return an;
}


public void setAn(int an) {
this.an = an;
}


public int getSemestru() {
return semestru;
}


public void setSemestru(int semestru) {
this.semestru = semestru;
}



}

这就是 Hibernate 制作我的类(class)表的方式:

create table Courses (
courseNo varchar2(255 char) not null,
idDiscipline number(10,0) not null,
idProfessor number(10,0) not null,
semester number(10,0) not null,
year number(10,0) not null,
primary key (courseNo, idDiscipline, idProfessor)
)

因此,外键不会出现。请帮助我

最佳答案

尝试改变这一点:

@ManyToOne
@JoinColumn(name="IDDISCIPLINE")
private int idDiscipline;

@ManyToOne
@JoinColumn(name="IDPROFESSOR")
private int idProfessor;
private String courseNo;

对此:

@ManyToOne
@JoinColumn(name="idDiscipline")
private Discipline discipline;

@ManyToOne
@JoinColumn(name="idProfessor")
private Professor professor;
private String courseNo;

关于java - 在jpa中添加外键和复合主键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40820407/

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