gpt4 book ai didi

java - 注释ManyToMany唯一关键问题

转载 作者:行者123 更新时间:2023-11-29 18:03:17 25 4
gpt4 key购买 nike

我遇到了 hibernate 问题:

我有两个类协作者和协作,我希望它们之间有一个 ManyToMany 关系。

我的协作者类(class):

@Entity
@Table(name = "Collaborateur")
public class Collaborateur {

@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private Integer id;
private String lastname;
private String firstname;
private String email;

还有我的协作类(class):

@Entity
@Table(name = "Collaboration")
public class Collaboration {

@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private Integer id;

private String nameCollaboration;

@ManyToMany(targetEntity=Collaborator.class ,fetch = FetchType.EAGER)
private List<Collaborator> collaborators;

当我在列表协作中插入两次相同的协作者时,出现以下错误:

javax.servlet.ServletException: org.hibernate.exception.ConstraintViolationException: could not execute statement

或者更准确地说:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '2' for key 'UK_obmdv02oey7264a085m7baqbo'

事实上,它是说我不能为一个协作者分配两个不同的协作。

所以我打印了mysql为n-n关系创建的表的创建,我有这个:

CREATE TABLE `Collaboration_Collaborator` (
`Collaboration_id` int(11) NOT NULL,
`collaborators_id` int(11) NOT NULL,
UNIQUE KEY `UK_obmdv02oey7264a085m7baqbo` (`collaborators_id`),
KEY `FKlrsiogs304faydykt1xtgpk9k` (`Collaboration_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

所以我需要指定我的唯一 key 不仅是 collaborators_id,而且是 collaborators_id 和 collaborators_id。

我该如何继续?

最佳答案

您需要添加可连接注释

@ManyToMany(targetEntity=Collaborator.class ,fetch = FetchType.EAGER)
@JoinTable
private List<Collaborator> collaborators;

关于java - 注释ManyToMany唯一关键问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48139751/

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