Spring数据JPA在一个类中有多个oneToMany关系

时间:2018-05-30 20:34:14

标签: hibernate spring-data-jpa

我正在尝试创建一个拥有创建者,多个管理员和多个成员的群组实体。

这是我的实体类:

@Entity
@Table(name = "user_group")
@Data
public class Group implements IPublicEntity<Group> {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "group_id", updatable = false)
    @JsonIgnore
    private int id;

    @NotEmpty
    @Column(name = "group_name", unique = true)
    @Size(max = 50)
    private String groupName;

    @OneToOne(cascade = CascadeType.MERGE)
    @JoinColumn(name = "creator")
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "publicId")
    @JsonIdentityReference(alwaysAsId = true)
    ApplicationUser creator;

    @OneToMany(cascade = CascadeType.MERGE, orphanRemoval = true)
    @JoinTable(name = "group_admin",
            joinColumns = @JoinColumn(name = "group_id"),
            inverseJoinColumns = @JoinColumn(name = "app_user_id"))
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "publicId")
    @JsonIdentityReference(alwaysAsId = true)
    List<ApplicationUser> admins = new ArrayList<>();

    @OneToMany(cascade = CascadeType.MERGE, orphanRemoval =  true)
    @JoinTable(name = "group_member",
            joinColumns = @JoinColumn(name = "group_id"),
            inverseJoinColumns = @JoinColumn(name = "app_user_id"))
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "publicId")
    @JsonIdentityReference(alwaysAsId = true)
    List<ApplicationUser> members = new ArrayList<>();
}

sql table:

user_group (
  group_id   SERIAL PRIMARY KEY,
  group_name VARCHAR(50) NOT NULL,
  creator    INTEGER     NOT NULL REFERENCES app_user (app_user_id)
);

group_admin (
  group_id INTEGER NOT NULL REFERENCES user_group (group_id),
  user_id  INTEGER NOT NULL REFERENCES app_user (app_user_id),
  PRIMARY KEY (group_id, user_id)
);

group_member (
  group_id INTEGER NOT NULL REFERENCES user_group (group_id),
  user_id  INTEGER NOT NULL REFERENCES app_user (app_user_id),
  PRIMARY KEY (group_id, user_id)
);

当我尝试将一个成员添加到该组并保存该组时,它会抛出错误ERROR: column members0_.app_user_id does not exist Position: 2519。但我已经将应用程序设置为查看表group_member而不是默认表。为什么它仍然会抛出错误。

感谢。

0 个答案:

没有答案