JPA Self Join using JoinTable

后端 未结 3 2042
攒了一身酷
攒了一身酷 2020-12-12 03:33

I have 1 entity call Item in which I want to be able to link parent items to children. to use a join table to create a parent/child relationship. I haven\'t been able to g

3条回答
  •  借酒劲吻你
    2020-12-12 04:11

    After a lot of reading on JPA 2.0 I figured out that I needed a newer version of Eclipselink 2.3+ in order to support what I was trying to do. Here is the code that actually worked in my case, but it will not work due to our dependency on an older version [EclipseLink 2.0.2]. Also another project's is still using Toplink-essentials JPA 1.0 which again does like this notation.

    public class Item {
    
    @ManyToOne(optional = true, fetch = FetchType.LAZY)
    @JoinTable(name = "ITEMINCENTIVESMAPPING", 
            joinColumns = { @JoinColumn(name = "INCENTIVEITEMID", referencedColumnName = "ITEMID", insertable = false, updatable = false) }, 
            inverseJoinColumns = { @JoinColumn(name = "ITEMID", referencedColumnName = "ITEMID", insertable = false, updatable = false) } )
    private Item parentItem;
    
    @OneToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "ITEMINCENTIVESMAPPING", 
            joinColumns = { @JoinColumn(name = "INCENTIVEITEMID", referencedColumnName = "ITEMID") }, 
            inverseJoinColumns = { @JoinColumn(name = "ITEMID", referencedColumnName = "ITEMID") } )
    private List items;
    
    }
    

提交回复
热议问题