使用 JoinTable 的 JPA 自联接

jto*_*fer 1 spring dao jpa toplink-essentials jpa-2.0

我有 1 个实体调用 Item,我希望能够将父项链接到子项。使用连接表来创建父/子关系。我一直无法获得任何好的文档。因此,如果有人有任何想法,我会全神贯注。

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

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;
}
Run Code Online (Sandbox Code Playgroud)

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

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)
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激。

Mic*_*aev 5

尝试@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;
Run Code Online (Sandbox Code Playgroud)