简体   繁体   English

使用JoinTable进行JPA自我联接

[英]JPA Self Join using JoinTable

I have 1 entity call Item in which I want to be able to link parent items to children. 我有1个实体调用项,希望在其中将父项链接到子项。 to use a join table to create a parent/child relationship. 使用联接表创建父/子关系。 I haven't been able to get any good documentation on. 我还没有得到任何好的文档。 So if anyone has any thoughts I'm all ears. 因此,如果有人有任何想法,我都会耳熟。

Here is what I have... which works most of the time. 这就是我所拥有的……大多数情况下都可以使用。

public class Item implements java.io.Serializable {
     @Id
     private Long id;

     @ManyToOne(optional = true, fetch = FetchType.LAZY)
     @JoinTable(name = "ITEMTOITEM", joinColumns = { @JoinColumn(name = "ITEMID") }, inverseJoinColumns = { @JoinColumn(name = "PARENTITEMID") } )
     private Item parent;

     @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
     private List<Item> children;
}

At times when I want to bring back objects that are tied to this item table I am getting an error of the following: 有时当我想带回绑定到该项目表的对象时,出现以下错误:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.webflow.engine.ActionExecutionException: Exception thrown executing [AnnotatedAction@6669ff5 targetAction = com.assisted.movein.web.common.nav.NavAction@6edf74b7, attributes = map['method' -> 'handleEntry']] in state 'oneTimeChargesAndFeesView' of flow 'in-flow' -- action execution attributes were 'map['method' -> 'handleEntry']'; nested exception is Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.0.1 (Build b04-fcs (04/11/2008))): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: ORA-00904: "PARENTITEM_ITEMID": invalid identifier

 Error Code: 904
 Call: SELECT ITEMID, ITEMSHORTDESC, EFFENDDATE, ITEMDESC, PARENTITEM_ITEMID,  ITEMTYPECODE FROM ITEM WHERE (ITEMID = ?)
    bind => [1250]
Query: ReadObjectQuery(com.domain.Item)

Any help would be appreciated. 任何帮助,将不胜感激。

Try to use @JoinColumn instead: 尝试使用@JoinColumn代替:

 @ManyToOne(optional = true, fetch = FetchType.LAZY)
 @JoinColumn(name = "PARENTITEMID", referencedColumnName = "ITEMID")
 private Item parent;

 @OneToMany(
        cascade = {CascadeType.ALL},
        orphanRemoval = true,
        fetch = FetchType.LAZY
 )
 @JoinColumn(name = "PARENTITEMID")
 private List<Item> children;

I believe @JoinTable is only allowed on a @OneToOne or @OneToMany or @ManyToMany, I don't think it makes any sense on a @ManyToOne. 我相信@JoinTable仅允许在@OneToOne或@OneToMany或@ManyToMany上使用,我认为在@ManyToOne上没有任何意义。 Use a @JoinColumn, or a @OneToOne. 使用@JoinColumn或@OneToOne。

Also your @OneToMany does not make sense, the mappedBy must be an attribute and you have no parentItem, it should be parent, and parent should use a @JoinColumn. 同样,您的@OneToMany没有任何意义,mappedBy必须是一个属性,并且您没有parentItem,它应该是parent,并且parent应该使用@JoinColumn。

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. 在对JPA 2.0进行了大量阅读之后,我发现我需要一个较新版本的Eclipselink 2.3+才能支持我想做的事情。 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]. 这是在我的情况下实际可用的代码,但是由于我们对较早版本[EclipseLink 2.0.2]的依赖,因此无法使用。 Also another project's is still using Toplink-essentials JPA 1.0 which again does like this notation. 同样,另一个项目仍在使用Toplink-essentials JPA 1.0,它也确实喜欢这种表示法。

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<Item> items;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM