java 如何在没有索引列的情况下通过 XML 设置 List 对象的“一对多”映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6047137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set up "one-to-many" mapping of List objects by XML without index column
提问by zono
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.
我可以通过使用注释来设置 List 对象的“一对多”映射,但使用 XML 则不然。你能告诉我如何使用 XML 映射进行设置吗?任何帮助将不胜感激。
Question. Do I need "INDEX" column when I associate some List objects using XML mapping?
问题。当我使用 XML 映射关联某些 List 对象时,是否需要“INDEX”列?
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:
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>
回答by matt b
A <list>
in a Hibernate Mapping XML file requires a <list-index>
, since you are telling Hibernate that you want to map an orderedcollection.
一个<list>
在Hibernate映射XML文件要求<list-index>
,因为你是在告诉Hibernate的要映射的有序集合。
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 to Set
, a <set>
:
如果您不关心集合中元素的位置,则应该使用 a <bag>
,或者如果您将 Java 类中的集合类型更改为Set
, a <set>
:
If your table does not have an index column, and you still wish to use
List
as the property type, you can map the property as a Hibernate<bag>
. A bag does not retain its order when it is retrieved from the database, but it can be optionally sorted or ordered.
如果您的表没有索引列,并且您仍希望
List
用作属性类型,则可以将该属性映射为 Hibernate<bag>
。从数据库中检索包时不会保留其顺序,但可以选择对其进行排序或排序。