Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up "one-to-many" mapping of List objects by XML without index column

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>
like image 660
zono Avatar asked May 18 '11 15:05

zono


People also ask

Which of the following annotation is used for one to many mapping?

Hibernate One To Many Mapping Annotation Model Classes Most important point in above class is the ManyToOne annotation on Cart1 class variable and JoinColumn annotation to provide the column name for mapping. That's it for one to many mapping in hibernate using annotation in model classes.

How you will create OneToMany relationship in Hibernate?

The way we do it in code is with @OneToMany. Let's map the Cart class to the collection of Item objects in a way that reflects the relationship in the database: public class Cart { //... @OneToMany(mappedBy="cart") private Set<Item> items; //... }

What is O R mapping how it is implemented using Hibernate explain with example?

If an entity or class has collection of values for a particular variable, then we can map those values using any one of the collection interfaces available in java. Hibernate can persist instances of java. util. Map, java.


1 Answers

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.

like image 152
matt b Avatar answered Oct 04 '22 19:10

matt b