Hibernate Framework : ORM Tool :: Hibernate Collection Mapping Example
Collection Mapping Intro |
Understanding Collection Mapping
- As we know , collection framework that is used to store a group of objects into a single unit.
- This collection framework has the interfaces like Set, List, and Map that are used in Hibernate applications to persists objects of POJO class in database tables.
- The Hibernate O/R mapping framework provides the <set>, there is also <list>,
- <map>, <bag>, <array> mapping elements that allow you to work with various objects.
These elements are know as collection mapping elements.
<map | set | list | bag
name="propertyName"
table="table_name"
schema="schema_name"
lazy="true|extra|false"
inverse="true|false"
cascade="all|none|save-update|delete|all-delete-orphan|delet e-orphan"
sort="unsorted|natural|comparatorClass"
order-by="column_name asc|desc"
where="arbitrary sql where condition"
fetch="join|select|subselect"
batch-size="N"
access="field|property|ClassName"
optimistic-lock="true|false"
mutable="true|false"
node="element-name|."
embed-xml="true|false">
<key .... />
<map-key .... />
<element .... />
- name: the collection property name
- table (optional – defaults to property name): the name of the collection table. It is not used for one-to-many associations.
- schema (optional): the name of a table schema to override the schema declared on the root element
- lazy (optional – defaults to true): disables lazy fetching and specifies that the association is always eagerly fetched. It can also be used to enable "extra-lazy" fetching where most operations do not initialize the collection. This is suitable for large collections.
- inverse (optional – defaults to false): marks this collection as the "inverse" end of a bidirectional association.
- cascade (optional – defaults to none): enables operations to cascade to child entities.
- sort (optional): specifies a sorted collection with natural sort order or a given comparator class.
- order-by (optional, JDK1.4 only): specifies a table column or columns that define the iteration order of the Map, Set or bag, together with an optional asc or desc.
- where (optional): specifies an arbitrary SQL WHERE condition that is used when retrieving or removing the collection. This is useful if the collection needs to contain only a subset of the available data.
- fetch (optional, defaults to select): chooses between outer-join fetching, fetching by sequential select, and fetching by sequential subselect.
- batch-size (optional, defaults to 1): specifies a "batch size" for lazily fetching instances of this collection.
- access (optional – defaults to property): the strategy Hibernate uses for accessing the collection property value.
- optimistic-lock (optional – defaults to true): specifies that changes to the state of the collection results in increments of the owning entity’s version. For one-to-many associations you may want to disable this setting.
- mutable (optional – defaults to true): a value of false specifies that the elements of the collection never change. This allows for minor performance optimization in some cases.
Here are some examples
1. <set>
A set element is used to store an unordered and unique collection of values. A set of strings:
<set name="names" table="person_names">
<key column="person_id"/>
<element column="person_name" type="string"/>
</set>
Or
<set name="emailaddress" table="EMP_email_Address">
<key column="emp_id"/>
<element column="email_address" type="string"/>
</set>
2. <bag>
The <bag> element is used to store unordered and duplicate collection of objects. A bag containing integers with an iteration order determined by the order-by attribute:
<bag name="sizes" table="item_sizes" order-by="size asc">
<key column="item_id"/>
<element column="size" type="integer"/>
</bag>
3. <array>
An array of entities, in this case, a many-to-many association:
<array name="addresses"
table="PersonAddress"
cascade="persist">
<key column="personId"/>
<list-index column="sortOrder"/>
<many-to-many column="addressId" class="Address"/>
</array>
4. <map>
A map element is used to store a collection of objects, values, or elements in the key/value pairs. A map from string indices to dates:
<map name="holidays" table="holidays" schema="dbo"
order-by="hol_name asc">
<key column="id"/>
<map-key column="hol_name" type="string"/>
<element column="hol_date" type="date"/>
</map>
5. <list>
A list element is used to store a collection of objects, values, or elements that can contains duplicate elements.
<list name="carComponents" table="CarComponents">
<key column="carId"/>
<list-index column="sortOrder"/>
<composite-element class="CarComponent">
<property name="price"/>
<property name="type"/>
<property name="serialNumber" column="serialNum"/>
</composite-element>
</list>
set/map/bag/list |
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">jdbc:mysql://localhost:3306/javaskoolDB</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="pfname">javaskoolMySQLDriver</property>
<property name="connection.password">admin</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping resource="com/javaskool/item.hbm.xml" />
</session-factory>
</hibernate-configuration>
Download Examples |
Click the below Link to download complete Example
Recent Comments