gpt4 book ai didi

java - 多对多、Hibernate 问题、AssociationOverrides

转载 作者:行者123 更新时间:2023-12-01 18:56:56 25 4
gpt4 key购买 nike

我在理解 hibernate 的多对多部分时遇到问题:(。

我正在尝试创建一些表,但我不知道如何在它们之间创建有效的关系。

我有表 UserCursOrarCurs,然后我有中间表 Prezenta

这是我的结构。

用户CursID

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

奥拉尔库斯:

@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 表中添加了这三列

Hibernate:更改表 prezenta 添加约束 FK_8ndmk871bbblvtt50y0qhrvl7 外键 (id_orar) 引用 OrarCurs (oc_id)

Hibernate:alter table prezenta添加约束FK_8vv4yxxpxc85vew0ox779r2kn外键(userCurs_id_curs,userCurs_id_user)引用user_curs(id_curs,id_user)

基本上,该表具有以下列:id_oraruserCurs_id_cursuserCurs_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
}

关于java - 多对多、Hibernate 问题、AssociationOverrides,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59666733/

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