1

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

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

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)

任何帮助,将不胜感激。

4

3 回答 3

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;
于 2013-03-05T08:11:33.023 回答
1

在大量阅读 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;

}
于 2013-03-21T19:14:19.663 回答
0

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

此外,您的@OneToMany 没有意义,mappedBy 必须是一个属性并且您没有 parentItem,它应该是父项,并且父项应该使用 @JoinColumn。

于 2013-03-05T15:38:49.813 回答