JPA Self Join使用JoinTable

时间:2013-03-05 05:12:36

标签: spring jpa jpa-2.0 dao toplink-essentials

我有一个实体调用项目,我希望能够将父项链接到子项。使用连接表来创建父/子关系。我无法获得任何好的文档。所以如果有人有任何想法我都会听到。

这就是我所拥有的......大部分时间都有效。

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;
}

有时当我想要带回与此项目表关联的对象时,我收到以下错误:

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)

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:4)

尝试改为使用@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;

答案 1 :(得分:0)

我相信@JoinTable只允许在@OneToOne或@OneToMany或@ManyToMany上使用,我认为它对@ManyToOne没有任何意义。使用@JoinColumn或@OneToOne。

你的@OneToMany也没有意义,mappedBy必须是一个属性而你没有parentItem,它应该是parent,而parent应该使用@JoinColumn。

答案 2 :(得分:0)

经过对JPA 2.0的大量阅读后,我发现我需要更新版本的Eclipselink 2.3+才能支持我想要做的事情。以下是在我的情况下实际工作的代码,但由于我们依赖于旧版本[EclipseLink 2.0.2],它将无法工作。另外一个项目仍在使用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;

}