我可以使用注释设置List对象的“一对多”映射,但是不使用 XML.你能告诉我如何设置使用 XML映射吗?任何帮助将不胜感激. 题.当我使用XML映射关联一些List对象时,是否需要“INDEX”列?
题.当我使用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>A< list>在Hibernate Mapping XML文件中需要< list-index>,因为你告诉Hibernate你想要 map an ordered collection.
如果您不关心集合中元素的位置,您应该使用< bag>,或者如果您将Java类中的集合类型更改为Set,则< 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.