0

I am trying to learn one to many Mapping in Hibernate .

The below are my two java classes Shelf.java

    public class Shelf {
    private Integer id;
    private String code;
    private Set<Book> books = new HashSet<Book>();

//setter - getter methods

}


Book.java

    public class Book {
    private String name;
    private Integer id;
    private Shelf shelf;

//setter - getter methods
}

I have following queries with respect to the above code

  1. Should be treat Shelf as the Master Table and Book as the Child Table ?? (Because Shelf.java contains many Objects of Object Book ?? or there isn't any such thumb rule to decide which is Master and Child Table )

2.

 <many-to-one name="shelf" class="Shelf" >
            <column name="SHELF_ID" not-null="true"></column>
        </many-to-one>

Inside hbm Mapping files in Book.hbm.xml file we have

why many-to-one tag here ??

4

2 に答える 2

3

[1] In a 1-N relationship like that what you can configure is which should be the owner side. The owner side is responsible for managing the relationship, it's the programmer's responsibility to set the other side, so the object graph will be consistent. (If you forget to do this, Hibernate will save the relationship, however your object graph will be temporarily inconsistent).

In this case, the owning side could be either Shelf or Book. Unless you have some reason to choose otherwise, it is more logical to pick Shelf as the owner, it will make your life easier when working with detached entities later on.

[2] On the Book side you need a Many-To-One relationship, on the Shelf side, you need a One-To-Many relationship, see in the below mapping. The reason you need this both is to let Hibernate know that the two ends of the relationship should be attached to form one relationship and should NOT be handled as separate (one sided) relationships.

Inside the .hbm file, you do:

<class name="com.company.Shelf" table="SHELF">
        <id name="id" type="integer" column="ID">
            <generator class="increment" />
        </id>
        <property name="code" type="string" not-null="true" length="100" column="CODE" />
        <set name="books" table="BOOKS" cascade="all">
            <key column="SHELF_ID" />
            <one-to-many class="com.company.Book" />
        </set>
</class>

<class name="com.company.Book" table="BOOK">
        <id name="id" type="int" column="id">
            <generator class="increment" />
        </id>
        <property name="name" type="java.lang.String" column="name" length="255" />
        <many-to-one name="shelf" class="com.company.shelf" column="SHELF_ID" />
</class>
于 2012-06-20T12:11:34.557 に答える
2
  1. Its better to call it as a parent-child relation then master-child relation

  2. Relations in hibernate are unidirectional. What this means is when you make a one-to-many relation in Shelf, many-to-one relation is not automatically created in Book classes. You need to explicitly create a many-to-one relation for Book if you need one.

于 2012-06-20T12:20:44.773 に答える