如何设置“一对多”通过 XML 映射 List 对象,无需索引列
我可以使用注释设置 List 对象的“一对多”映射,但使用 XML 则不行。您能告诉我如何使用 XML 映射进行设置吗?任何帮助将不胜感激。
问题。当我使用 XML 映射关联某些 List 对象时,是否需要“INDEX”列?
注释映射->它按预期工作:
@Entity
@Table(name = "ITEM")
public class Item {
@Id
@Column(name = "ID")
private Long id;
@Column(name = "NAME")
private String name;
@OneToMany(targetEntity = ItemDetail.class)
@JoinColumn(name = "ITEM_ID")
private List<ItemDetail> itemDetails;
@Entity
@Table(name = "ITEM_DETAIL")
public class ItemDetail {
@Id
@Column(name = "ID")
private Long id;
@Column(name = "NAME")
private String name;
@Column(name = "ITEM_ID")
private Long itemId;
XML 映射 ->它没有按预期工作。发生“解析 XML 时出错”错误。似乎需要“INDEX列”信息:
<hibernate-mapping>
<class name="jp.sample.entity.Item" table="ITEM">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="NAME" />
</property>
<list name="itemDetails" cascade="all">
<key column="ITEM_ID" />
<one-to-many class="jp.sample.entity.ItemDetail" />
</list>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="jp.sample.entity.ItemDetail" table="ITEM_DETAIL">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="NAME" />
</property>
<property name="itemId" type="java.lang.Long">
<column name="ITEM_ID" />
</property>
</class>
</hibernate-mapping>
I can set up "one-to-many" mapping of List objects by using annotations, however using XML is not. Could you tell me how to set up using XML mapping. Any help will be appreciated.
Question. Do I need "INDEX" column when I associate some List objects using XML mapping?
annotation mapping -> It works as expected:
@Entity
@Table(name = "ITEM")
public class Item {
@Id
@Column(name = "ID")
private Long id;
@Column(name = "NAME")
private String name;
@OneToMany(targetEntity = ItemDetail.class)
@JoinColumn(name = "ITEM_ID")
private List<ItemDetail> itemDetails;
@Entity
@Table(name = "ITEM_DETAIL")
public class ItemDetail {
@Id
@Column(name = "ID")
private Long id;
@Column(name = "NAME")
private String name;
@Column(name = "ITEM_ID")
private Long itemId;
XML mapping -> It does not work as expected. "Error parsing XML" error is occurred. It seems to need "INDEX column" information:
<hibernate-mapping>
<class name="jp.sample.entity.Item" table="ITEM">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="NAME" />
</property>
<list name="itemDetails" cascade="all">
<key column="ITEM_ID" />
<one-to-many class="jp.sample.entity.ItemDetail" />
</list>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="jp.sample.entity.ItemDetail" table="ITEM_DETAIL">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="NAME" />
</property>
<property name="itemId" type="java.lang.Long">
<column name="ITEM_ID" />
</property>
</class>
</hibernate-mapping>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Hibernate 映射 XML 文件中的
需要
,因为您告诉 Hibernate 您想要 映射一个有序集合。如果您不关心集合中元素的位置,则应该使用
,或者将 Java 类中的集合类型更改为Set
>,
:A
<list>
in a Hibernate Mapping XML file requires a<list-index>
, since you are telling Hibernate that you want to map an ordered collection.If you do not care about the position of elements in the collection, you should be using a
<bag>
, or if you change the collection type in the Java class toSet
, a<set>
: