我有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)
尝试使用@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;
在大量阅读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;
}
我相信@JoinTable只允许在@OneToOne或@OneToMany或@ManyToMany上,我不认为它在@ManyToOne上有任何意义。使用@JoinColumn或@OneToOne。
你的@OneToMany也没有意义,mappedBy必须是一个属性,你没有parentItem,它应该是parent, parent应该使用@JoinColumn。