Hibernate Basics
Hibernate Setup
Hibernate Notes
Hibernate Mapping using XML
Collection Bag Mapping in Hibernate using XML
When using List<> in our Persistent class then we have option to either use list or bag tag in mapping file for mapping. Bag is similar to list but it does not require index tag.
Observe the code bellow. The first snippet of code showing list mapping for one-to-many relation using list tag in mapping file and second showing same mapping and same relation using bag tag.
Mapping using list tag
<list name="orders" cascade="all"> <!--Collection tag--> <key column="customer_frk_id" not-null="true" on-delete="cascade" update="true" unique="false" /> <index column="type"></index> <!--necessary index in list tag--> <one-to-many class="Orders"/> <!--relation--> </list>
Mapping using bag tag
<bag name="orders" cascade="all"> <!--Collection tag--> <key column="customer_frk_id" not-null="true" on-delete="cascade" update="true" unique="false" /> <!--<index column="type"></index>--> <!--doesn't accept index tag--> <one-to-many class="Orders"/> <!--relation--> </bag>
In the example above of bag mapping It does not accept index tag in bag.
Recent Posts