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

泄露秘密 提交于 2020-05-11 05:47:25

问题


I have the following (simplified) situation:

Fields for TABLE A:

  • ID
  • COMMONID

Fields for TABLE AB:

  • AID
  • BID COMMONID

Fields for TABLE B:

  • ID
  • COMMONID

and want to map it with entities using a OneToMany like this (in the master class mapped on table 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>();

Building the session, I obtain the following error:

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

What am I doing wrong? Consider that I'm new to Hibernate.


回答1:


Try to add insertable, and updateable like this

@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>();

But I'm not sure whether you will able to update this set, my use case was read only.



来源:https://stackoverflow.com/questions/14734235/org-hibernate-mappingexception-repeated-column-in-mapping-for-collection-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!