1

The hibernate document says that for unidirectional one-to-many association with join table is recommended instead of without join table.

http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/associations.html#assoc-unidirectional-12m

A unidirectional one-to-many association on a foreign key is an unusual case, and is not recommended.

<class name="Person">
        <id name="id" column="personId">
            <generator class="native"/>
        </id>
        <set name="addresses">
            <key column="personId" 
                not-null="true"/>
            <one-to-many class="Address"/>
        </set>
    </class>

    <class name="Address">
        <id name="id" column="addressId">
            <generator class="native"/>
        </id>
    </class>

http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/associations.html#assoc-unidirectional-12m

A unidirectional one-to-many association on a join table is the preferred option. Specifying unique="true", changes the multiplicity from many-to-many to one-to-many.

<class name="Person">
    <id name="id" column="personId">
        <generator class="native"/>
    </id>
    <set name="addresses" table="PersonAddress">
        <key column="personId"/>
        <many-to-many column="addressId"
            unique="true"
            class="Address"/>
    </set>
</class>

<class name="Address">
    <id name="id" column="addressId">
        <generator class="native"/>
    </id>
</class>

1) Why it is not recommended to use one-to-many with out join table?

2) Also can't we use one-to-many tag instead of many-to-many tag for defining the relationship with join table?

I am new to Hibernate, please help me to understand this.

Chaitanya
  • 15,403
  • 35
  • 96
  • 137
  • 1
    possible duplicate of [Hibernate unidirectional one to many association - why is a join table better?](http://stackoverflow.com/questions/1307203/hibernate-unidirectional-one-to-many-association-why-is-a-join-table-better) – jocki Jan 06 '14 at 16:21
  • @newbie I have gone through that post already, but I was not able to understand the example given there so I am expecting experts in SO to give a simple way of explanation. – Chaitanya Jan 06 '14 at 16:31
  • 1
    @user2065083 the explanation in the linked post is quite clear. let's say a User, a Customer, A Vendor, a Contact, a Friend and a Location all have a OneToMany association with Address: you don't want address to have 6 foreign keys and to change its schema each time a new entity needs an address. So you simply add a join table instead and leave the address table untouched. The Address entity doesn't know about its parents, so the address table shouldn't know about them either. – JB Nizet Jan 06 '14 at 16:42
  • Regarding XML mapping: completely forget bout it, and use annotations. We're not in 1005 anymore. And use a recent version of Hibernate. 3.3 is obsolete. – JB Nizet Jan 06 '14 at 16:43
  • @JBNizet, Thanks for responding for my first query, can you please let me know the response for my second question also. I am checking the Hibernate 4.2 Document (http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch08.html), even there also the explanation given is same as 3.3 – Chaitanya Jan 07 '14 at 07:26
  • XML mapping is still supported, and the documentation is correct, but you shouldn't use XML mapping, so you shouldn't care about how to use it. Use JPA annotations to define the mapping. – JB Nizet Jan 07 '14 at 07:50

0 Answers0