Hibernate 中的自引用实体
我有一个 Action 实体,它可以将其他 Action 对象作为双向一对多关系中的子对象。 问题是 Hibernate 输出以下异常:
“集合映射中的重复列:DbAction.childs 列:actionId”
映射代码下方:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="DbAction" table="actions">
<id name="actionId" type="short" />
<property not-null="true" name="value" type="string" />
<set name="childs" table="action_action" cascade="all-delete-orphan">
<key column="actionId" />
<many-to-many column="actionId" unique="true" class="DbAction" />
</set>
<join table="action_action" inverse="true" optional="false">
<key column="actionId" />
<many-to-one name="parentAction" column="actionId" not-null="true" class="DbAction" />
</join>
</class>
</hibernate-mapping>
I have an Action
entity, that can have other Action
objects as child in a bidirectional one-to-many relationship.
The problem is that Hibernate outputs the following exception:
"Repeated column in mapping for collection: DbAction.childs column: actionId"
Below the code of the mapping:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="DbAction" table="actions">
<id name="actionId" type="short" />
<property not-null="true" name="value" type="string" />
<set name="childs" table="action_action" cascade="all-delete-orphan">
<key column="actionId" />
<many-to-many column="actionId" unique="true" class="DbAction" />
</set>
<join table="action_action" inverse="true" optional="false">
<key column="actionId" />
<many-to-one name="parentAction" column="actionId" not-null="true" class="DbAction" />
</join>
</class>
</hibernate-mapping>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为您为同一个表多次声明了
name="actionId"
。That's because you have
name="actionId"
declared more than once for the same table.正如armandino所建议的,我尝试将列名替换为“parentActionId”,并且它有效:
As armandino suggested, i tried to substitute the column name to "parentActionId", and it works: