7

我有以下(简化)情况:

表 A 的字段:

  • ID
  • 常见的

表 AB 的字段:

  • 援助
  • 投标共同

表 B 的字段:

  • ID
  • 常见的

并希望使用这样的 OneToMany 将其与实体映射(在表 A 上映射的主类中):

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = false, fetch = FetchType.LAZY)
@JoinTable(name = "AB", 
joinColumns= {
    @JoinColumn(name = "AID", referencedColumnName="ID"), 
    @JoinColumn(name = "COMMONID", referencedColumnName="COMMONID")},
inverseJoinColumns = {
    @JoinColumn(name = "BID", referencedColumnName="ID"), 
    @JoinColumn(name = "COMMONID", referencedColumnName="COMMONID")})
private Set<MyClassForB> list= new HashSet<MyClassForB>();

构建会话,我收到以下错误:

org.hibernate.MappingException: Repeated column in mapping for collection using @JoinTable list column: COMMONID

我究竟做错了什么?考虑到我是 Hibernate 的新手。

4

2 回答 2

1

尝试像这样添加可插入和可更新

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = false, fetch = FetchType.LAZY)
@JoinTable(name = "AB", 
joinColumns= {
    @JoinColumn(name = "AID", referencedColumnName="ID", insertable = false, updatable = false), 
    @JoinColumn(name = "COMMONID", referencedColumnName="COMMONID", insertable = false, updatable = false)},
inverseJoinColumns = {
    @JoinColumn(name = "BID", referencedColumnName="ID", insertable = false, updatable = false), 
    @JoinColumn(name = "COMMONID", referencedColumnName="COMMONID", insertable = false, updatable = false)})
private Set<MyClassForB> list= new HashSet<MyClassForB>();

但是我不确定你是否能够更新这个集合,我的用例是只读的。

于 2017-03-14T10:04:22.883 回答
0

你的 ER 图 假设您允许您的 JPA 提供程序(例如 Hibernate)创建表...试试这个

Class A{
    @ManyToMany
    @JoinTable(
    name = "AB", 
    joinColumns = @JoinColumn(name = "A_Id"), 
    inverseJoinColumns = @JoinColumn(name = "B_Id")
    )
     private Set<B> blist= new HashSet<B>();
}

Class B{
    
     @ManyToMany(cascade = CascadeType.ALL, mappedBy = "A_Id")
    private Set<A> blist= new HashSet<A>();
}
于 2021-08-09T03:05:46.103 回答