@ManyToMany annotation spring boot JPA with extra column and composite key ,Error inserting

huangapple go评论80阅读模式
英文:

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

}


<details>
<summary>英文:</summary>

I&#39;m using spring boot 2.2.5 with  Jpa and JDK 11 and for my database i&#39;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&#39;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 = &quot;FK_USER_ID&quot;)
@MapsId(&quot;fkUserId&quot;)
@ManyToOne(fetch = FetchType.LAZY)
public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

@JoinColumn(name = &quot;FK_GROUP_ID&quot;)
@MapsId(&quot;fkGroupId&quot;)
@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 = &quot;FK_USER_ID&quot;)
@Id
public long getFkUserId() {
    return fkUserId;
}

public void setFkUserId(long fkUserId) {
    this.fkUserId = fkUserId;
}
@Column(name = &quot;FK_GROUP_ID&quot;)
@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 = &quot;GROUP_ID&quot;)
public long getGroupId() {
    return groupId;
}

public void setGroupId(long groupId) {
    this.groupId = groupId;
}

@Basic
@Column(name = &quot;DESIGN&quot;)
public String getDesign() {
    return design;
}

public void setDesign(String design) {
    this.design = design;
}


@OneToMany(mappedBy = &quot;group&quot;,cascade = {CascadeType.ALL})
public Collection&lt;UserGroup&gt; getUserGroup() {
    return userGroup;
}

public void setUserGroup(Collection&lt;UserGroup&gt; 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 = &quot;USER_ID&quot;)
public long getUserId() {
    return userId;
}

public void setUserId(long userId) {
    this.userId = userId;
}

@Basic
@Column(name = &quot;DESIGN&quot;)
public String getDesign() {
    return design;
}

public void setDesign(String design) {
    this.design = design;
}


@OneToMany(mappedBy = &quot;user&quot;,cascade = {CascadeType.ALL})
public Collection&lt;UserGroup&gt; getUserGroup() {
    return userGroup;
}

public void setUserGroup(Collection&lt;UserGroup&gt; 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 = &quot;USER&quot;, schema = &quot;TEST&quot;, catalog = &quot;&quot;)
public class User {
    private long userId;
    private String nom;
    private Collection&lt;UserGroup&gt; userGroups;

    @Id
    @Column(name = &quot;USER_ID&quot;)
    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    @Basic
    @Column(name = &quot;DESIGN&quot;)
    public String getDesign() {
        return design;
    }

    public void setDesign(String design) {
        this.design = design;
    }


    @OneToMany(mappedBy = &quot;user&quot;, cascade = {CascadeType.ALL})
    public Collection&lt;UserGroup&gt; getUserGroups() {
        return userGroups;
    }

    public void setUserGroup(Collection&lt;UserGroup&gt; 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.

huangapple
  • 本文由 发表于 2020年10月22日 18:22:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64480267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定