gpt4 book ai didi

java - 使用@OneToOne属性设置对象可以,但不适用于@ManyToMany对象

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

这是我的两个类的代码:

@Entity
public class FicheMetier implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int Id;
private String Name;
@OneToOne
private Employe employe;
private String description;
@ManyToOne(cascade = CascadeType.ALL)
private MatriceComp matricecomp;




@Entity
public class MatriceComp implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int Id;
@Enumerated(EnumType.STRING)
@NotNull
private Category category;
private String Description;
@ManyToMany(cascade = CascadeType.ALL)
private List<Competance> competances;


这是我正在尝试运行的服务:

@Override
public void affecterMat(MatriceComp matrice,FicheMetier ficheMetier) {

FicheMetier us= em.find(FicheMetier.class,ficheMetier.getId());

us.setMatricecomp(matrice.getId());

}




当我尝试使用矩阵设置属性@ManyToOne matricecomp时,我得到EJBTransactionRolledbackException。
但是!如果将其更改为@OneToOne matricecomp,并且我使用matrice设置了属性,则可以正常工作

最佳答案

用这种方式

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "MatriceComp_Competance",
joinColumns = { @JoinColumn(name = "id") },
inverseJoinColumns = { @JoinColumn(name = "id") })
private List<Competance> MatriceCompCompetances= new ArrayList<Competance>();

关于java - 使用@OneToOne属性设置对象可以,但不适用于@ManyToMany对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58582665/

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