gpt4 book ai didi

java - Spring Boot JPA ManyToMany MappingException

转载 作者:行者123 更新时间:2023-11-30 06:30:26 30 4
gpt4 key购买 nike

我有两个实体想要通过 ManyToMany 关联。我找到了很多示例(例如 https://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Example_of_a_ManyToMany_relationship_annotationhttps://vladmihalcea.com/the-best-way-to-use-the-manytomany-annotation-with-jpa-and-hibernate/ ),甚至在我的代码中还有另一个地方,多对多关系似乎起作用,但到目前为止我仍然无法弄清楚这个。

enter image description here

我遇到以下异常:

Caused by: org.hibernate.MappingException: Foreign key (FKru66hm0369ayn8dvjj2rgxr9v:user_role [role_id])) must have same number of columns as the referenced primary key (user_role [user_id,role_id])

类用户

@Entity
@Table(name = "user")
public class User {

@Id
@GeneratedValue
@Column(name = "user_id")
private long id;

@ManyToMany
@JoinTable(
name = "user_role",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles;

// getters and setters
}

类(class)角色

@Entity
@Table(name = "user_role")
public class Role {

@Id
@GeneratedValue
@Column(name = "role_id")
private long id;

@ManyToMany(mappedBy = "roles")
private Set<User> users;

// getters and setters

}

最佳答案

检查this link !

无论如何,问题是你拥有类(class)角色

@Entity
@Table(name = "user_role")
public class Role {

也在用户类中

 @JoinTable(
name = "user_role",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id"))

两个地方, table 的名称是相同的......
这一定是错误的原因......

关于java - Spring Boot JPA ManyToMany MappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46278672/

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