如何设置“一对多”通过 XML 映射 List 对象,无需索引列

发布于 2024-11-07 21:27:39 字数 1938 浏览 0 评论 0原文

我可以使用注释设置 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 技术交流群。

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

发布评论

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

评论(1

若有似无的小暗淡 2024-11-14 21:27:39

Hibernate 映射 XML 文件中的 需要 ,因为您告诉 Hibernate 您想要 映射一个有序集合

如果您不关心集合中元素的位置,则应该使用 ,或者将 Java 类中的集合类型更改为 Set >,

如果您的表没有索引列,并且您仍然希望使用 List 作为属性类型,则可以将该属性映射为 Hibernate 。从数据库中检索包时,它不会保留其顺序,但可以选择对其进行排序或排序。

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 to 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文