英文:
@ManyToMany annotation spring boot JPA with extra column and composite key ,Error inserting
问题
我正在使用Spring Boot 2.2.5与JPA和JDK 11,我的数据库是Oracle 19c。
我的实体User和Group之间有一个多对多的关系。
在我的Oracle数据库中,有一个关联的(USER_GROUP)表,它有一个复合主键(user_ID, group_ID)。
USER_ID和GROUP_ID会在数据库中自动生成。
我希望当我在USER表中插入一个用户时,JPA也会在关联的USER_GROUP表中插入。
但是它总是返回“无法将null插入USER_ID”,我无法访问此用户ID,因为它在数据库中自动生成。
我的UserGroup类:
@Entity
@Table(name = "USER_GROUP", schema = "TEST", catalog = "")
public class UserGroup {
private GroupUserPK groupUserPK;
private User user;
private Group group;
@EmbeddedId
public GroupUserPK getGroupUserPK() {
return groupUserPK;
}
public void setGroupUserPK(GroupUserPK groupUserPK) {
this.groupUserPK = groupUserPK;
}
@JoinColumn(name = "FK_USER_ID")
@MapsId("fkUserId")
@ManyToOne(fetch = FetchType.LAZY)
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@JoinColumn(name = "FK_GROUP_ID")
@MapsId("fkGroupId")
@ManyToOne(fetch = FetchType.LAZY)
public Group getGroup() {
return group;
}
}
UserGroupPK类:
@Embeddable
public class GroupUserPK implements Serializable {
private static final long serialVersionUID = 1L;
private long fkUserId;
private long fkGroupId;
@Column(name = "FK_USER_ID")
@Id
public long getFkUserId() {
return fkUserId;
}
public void setFkUserId(long fkUserId) {
this.fkUserId = fkUserId;
}
@Column(name = "FK_GROUP_ID")
@Id
public long getFkGroupId() {
return fkGroupId;
}
public void setFkGroupId(long fkGroupId) {
this.fkGroupId = fkGroupId;
}
}
Group类:
@Entity
@Table(name = "GROUP", schema = "TEST", catalog = "")
public class Group {
private long groupId;
private String name;
private Collection<UserGroup> userGroup;
@Id
@Column(name = "GROUP_ID")
public long getGroupId() {
return groupId;
}
public void setGroupId(long groupId) {
this.groupId = groupId;
}
@Basic
@Column(name = "DESIGN")
public String getDesign() {
return design;
}
public void setDesign(String design) {
this.design = design;
}
@OneToMany(mappedBy = "group", cascade = {CascadeType.ALL})
public Collection<UserGroup> getUserGroup() {
return userGroup;
}
public void setUserGroup(Collection<UserGroup> userGroup) {
this.userGroup = userGroup;
}
}
User类:
@Entity
@Table(name = "USER", schema = "TEST", catalog = "")
public class User {
private long userId;
private String name;
private Collection
@Id
@Column(name = "USER_ID")
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
@Basic
@Column(name = "DESIGN")
public String getDesign() {
return design;
}
public void setDesign(String design) {
this.design = design;
}
@OneToMany(mappedBy = "user", cascade = {CascadeType.ALL})
public Collection<UserGroup> getUserGroup() {
return userGroup;
}
public void setUserGroup(Collection<UserGroup> userGroup) {
this.userGroup = userGroup;
}
}
<details>
<summary>英文:</summary>
I'm using spring boot 2.2.5 with Jpa and JDK 11 and for my database i'm using oracle 19c.
I have a ManyToMany relationship between my entities User and Group.
In my oracle database i have a associated (USER_GROUP) table with composite key (user_ID,group_ID).
USER_ID and GROUP_ID are automaticaly generated in the database.
I want when i insert a user in table USER JPA also insert in associated table USER_GROUP.
But it return always cannot insert null in USER_ID,and this user id i don't have access because it generated automatily in database.
MyClass UserGroup:
@Entity
@Table(name = "USER_GROUP", schema = "TEST", catalog = "")
public class UserGroup {
private GroupUserPK groupUserPK;
private User user;
private Group group;
@EmbeddedId
public GroupUserPK getGroupUserPK() {
return groupUserPK;
}
public void setGroupUserPK(GroupUserPK groupUserPK) {
this.groupUserPK = groupUserPK;
}
@JoinColumn(name = "FK_USER_ID")
@MapsId("fkUserId")
@ManyToOne(fetch = FetchType.LAZY)
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@JoinColumn(name = "FK_GROUP_ID")
@MapsId("fkGroupId")
@ManyToOne(fetch = FetchType.LAZY)
public Group getGroup() {
return group;
}
}
UserGroupPK
@Embeddable
public class GroupUserPK implements Serializable {
private static final long serialVersionUID = 1L;
private long fkUserId;
private long fkGroupId;
@Column(name = "FK_USER_ID")
@Id
public long getFkUserId() {
return fkUserId;
}
public void setFkUserId(long fkUserId) {
this.fkUserId = fkUserId;
}
@Column(name = "FK_GROUP_ID")
@Id
public long getFkGroupId() {
return fkGroupId;
}
public void setFkGroupId(long fkGroupId) {
this.fkGroupId = fkGroupId;
}
}
MyClass Group:
@Entity
@Table(name = "GROUP", schema = "TEST", catalog = "")
public class Group {
private long groupId;
private String nom;
private Collection<UserGroup> userGroup;
@Id
@Column(name = "GROUP_ID")
public long getGroupId() {
return groupId;
}
public void setGroupId(long groupId) {
this.groupId = groupId;
}
@Basic
@Column(name = "DESIGN")
public String getDesign() {
return design;
}
public void setDesign(String design) {
this.design = design;
}
@OneToMany(mappedBy = "group",cascade = {CascadeType.ALL})
public Collection<UserGroup> getUserGroup() {
return userGroup;
}
public void setUserGroup(Collection<UserGroup> userGroup) {
this.userGroup = userGroup;
}
}
MyClass User
@Entity
@Table(name = "USER", schema = "TEST", catalog = "")
public class User {
private long userId;
private String nom;
private Collection<UserGroup> userGroup;
@Id
@Column(name = "USER_ID")
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
@Basic
@Column(name = "DESIGN")
public String getDesign() {
return design;
}
public void setDesign(String design) {
this.design = design;
}
@OneToMany(mappedBy = "user",cascade = {CascadeType.ALL})
public Collection<UserGroup> getUserGroup() {
return userGroup;
}
public void setUserGroup(Collection<UserGroup> userGroup) {
this.userGroup = userGroup;
}
}
Any help please.
</details>
# 答案1
**得分**: 0
Edit
我通过在我的实体用户(User)中添加了一个 "addGroup" 方法来解决了这个问题。
我的实体用户(User)更新如下:
```java
@Entity
@Table(name = "USER", schema = "TEST", catalog = "")
public class User {
private long userId;
private String design;
private Collection<UserGroup> userGroups;
@Id
@Column(name = "USER_ID")
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
@Basic
@Column(name = "DESIGN")
public String getDesign() {
return design;
}
public void setDesign(String design) {
this.design = design;
}
@OneToMany(mappedBy = "user", cascade = {CascadeType.ALL})
public Collection<UserGroup> getUserGroups() {
return userGroups;
}
public void setUserGroups(Collection<UserGroup> userGroups) {
this.userGroups = userGroups;
}
public void addGroup(Group group) {
UserGroup userGroup = new UserGroup(this, group);
userGroups.add(userGroup);
group.getUserGroups().add(userGroup);
}
}
之后我们在 UserService 中调用了这个方法。
英文:
Edit
I solved this problem by adding "addGroup" method in my entity User.
My entity user updated as,
@Entity
@Table(name = "USER", schema = "TEST", catalog = "")
public class User {
private long userId;
private String nom;
private Collection<UserGroup> userGroups;
@Id
@Column(name = "USER_ID")
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
@Basic
@Column(name = "DESIGN")
public String getDesign() {
return design;
}
public void setDesign(String design) {
this.design = design;
}
@OneToMany(mappedBy = "user", cascade = {CascadeType.ALL})
public Collection<UserGroup> getUserGroups() {
return userGroups;
}
public void setUserGroup(Collection<UserGroup> userGroups) {
this.userGroups = userGroups;
}
public void addGroup(Group group) {
UserGroup userGroup = new UserGroup(this, group);
userGroups.add(userGroup);
group.getUserGroups().add(userGroup);
}
}
After that we called this method in UserService.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论