Hibernate 中的自引用实体

发布于 2024-08-28 11:09:40 字数 990 浏览 11 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仙女山的月亮 2024-09-04 11:09:40

这是因为您为同一个表多次声明了 name="actionId"

That's because you have name="actionId" declared more than once for the same table.

甜点 2024-09-04 11:09:40

正如armandino所建议的,我尝试将列名替换为“parentActionId”,并且它有效:

<?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="parentActionId" />
   <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="parentActionId" not-null="true" class="DbAction" />
  </join>
 </class>
</hibernate-mapping>

As armandino suggested, i tried to substitute the column name to "parentActionId", and it works:

<?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="parentActionId" />
   <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="parentActionId" not-null="true" class="DbAction" />
  </join>
 </class>
</hibernate-mapping>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文