我在many-to-many
部分:(了解休眠状态时遇到问题。
我正在尝试创建一些表,但是我不知道如何在它们之间创建有效的关系。
我有表UserCurs
和OrarCurs
,然后有中间表Prezenta
。
这是我的结构。
UserCursID
import javax.persistence.Embeddable;
import javax.persistence.ManyToOne;
@Embeddable
public class UserCursID implements java.io.Serializable {
private static final long serialVersionUID = -9120607274421816301L;
private User user;
private Curs cs;
@ManyToOne
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@ManyToOne
public Curs getCs() {
return cs;
}
public void setCs(Curs cs) {
this.cs = cs;
}
}
UserCurs(也是User和Curs的中间表):
@Entity
@Table(name="user_curs")
@AssociationOverrides({
@AssociationOverride(name="pk.user", joinColumns = @JoinColumn(name="id_user")),
@AssociationOverride(name="pk.cs", joinColumns = @JoinColumn(name="id_curs"))
})
public class UserCurs implements java.io.Serializable {
private static final long serialVersionUID = 4050660680047579957L;
private UserCursID pk = new UserCursID();
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer uc_id;
public Integer getUc_id() {
return uc_id;
}
public void setUc_id(Integer uc_id) {
this.uc_id = uc_id;
}
@EmbeddedId
public UserCursID getPk() {
return pk;
}
public void setPk(UserCursID pk) {
this.pk = pk;
}
}
OrarCurs:
@Entity
@Table(name="OrarCurs")
public class OrarCurs {
@Id
@Column(name="oc_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer oc_id;
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST})
@JoinColumn(name="curs_id")
private Curs orarcurs;
@Column(name="data_curs")
private Date dataCurs;
public Integer getOc_id() {
return oc_id;
}
public void setOc_id(Integer oc_id) {
this.oc_id = oc_id;
}
public Curs getOrarcurs() {
return orarcurs;
}
public void setOrarcurs(Curs orarcurs) {
this.orarcurs = orarcurs;
}
public Date getDataCurs() {
return dataCurs;
}
public void setDataCurs(Date dataCurs) {
this.dataCurs = dataCurs;
}
}
和PrezentaID:
import javax.persistence.Embeddable;
import javax.persistence.ManyToOne;
@Embeddable
public class PrezentaID implements java.io.Serializable{
private static final long serialVersionUID = -9120607274421816301L;
private UserCurs usercurs;
private OrarCurs orar;
@ManyToOne
public UserCurs getUserCurs() {
return usercurs;
}
public void setUserCurs(UserCurs usercurs) {
this.usercurs = usercurs;
}
@ManyToOne
public OrarCurs getOrar() {
return orar;
}
public void setOrar(OrarCurs orar) {
this.orar = orar;
}
}
与Prezenta类:
@Entity
@Table(name="prezenta")
@AssociationOverrides({
@AssociationOverride(name="pk1.usercurs", joinColumns = @JoinColumn(name="id_usercurs", referencedColumnName="usercurs_id")),
@AssociationOverride(name="pk1.orar", joinColumns = @JoinColumn(name="id_orar", referencedColumnName="oc_id"))
})
public class Prezenta implements java.io.Serializable{
private static final long serialVersionUID = 4050660680047579957L;
private PrezentaID pk1 = new PrezentaID();
private Boolean Present;
@EmbeddedId
public PrezentaID getPk1() {
return pk1;
}
public Boolean getPresent() {
return Present;
}
public void setPresent(Boolean present) {
Present = present;
}
public void setPk1(PrezentaID pk1) {
this.pk1 = pk1;
}
}
运行main.class之后,一切正常,所有表都已创建,但是在我的Prezenta表中添加了这三列
休眠:
alter table prezenta add constraint FK_8ndmk871bbblvtt50y0qhrvl7 foreign key (id_orar) references OrarCurs (oc_id)
休眠:
alter table prezenta add constraint FK_8vv4yxxpxc85vew0ox779r2kn foreign key (userCurs_id_curs, userCurs_id_user) references user_curs (id_curs, id_user)
基本上,该表具有以下列:
id_orar
,userCurs_id_curs
,userCurs_id_user
,但我不希望最后两列,我想要代替它们的是与uc_id
(来自UserCurs
)相对应的一列,而我不知道该怎么做。 最佳答案
您不必为此映射创建类Prezenta。您只需要两个要映射的类和对@ManyToMany进行注释。这是个好话题:
https://www.baeldung.com/hibernate-many-to-many Hibernate将使用您班级的pk创建联接表
@Entity
@Table(name="db table1 name")
Class1{
@Id
@Column(name="id in table 1")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer class1_id;
@JoinTable(
name = "class1_class2",
joinColumns = { @JoinColumn(name = "class1_id") },
inverseJoinColumns = { @JoinColumn(name = "class2_id") }
)
private List<Class2> list1;
}
@Entity
@Table(name="db table2 name")
Class2{
@Id
@Column(name="id in table 1")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer class1_id;
@ManyToMany(mappedBy = "list1")
private List<Class1> list2
}