|
|
|
HIBERNATE
TUTORIAL
HIBERNATE
- Hibernate Mapping In Depth
Hibernate
allows the mapping of Mapped tables with
the domain objects using the persistent
collection-valued fields. These fields
needs be declared as an interface type.
The actual interface can be java.util.Set,
java.util.Collection, java.util.List,
java.util.Map, java.util.SortedSet, java.util.SortedMap
or custom implementations of org.hibernate.usertype.UserCollectionType
Collections instances have the usual behavior
of value types. They are automatically
persisted when referenced by a persistent
object and automatically deleted when
unreferenced. If a collection is passed
from one persistent object to another,
its elements might be moved from one table
to another. Two entities may not share
a reference to the same collection instance.
Due to the underlying relational model,
collection-valued properties do not support
null value semantics;
public
class Product { |
|
private
String serialNumber;
private Set parts = new HashSet();
public Set getParts() { return parts;
}
void setParts(Set parts) { this.parts
= parts; }
public String getSerialNumber()
{ return serialNumber; }
void setSerialNumber(String sn)
{ serialNumber = sn; } |
} |
Collection Mapping
<map
|
|
name="propertyName"
table="table_name"
schema="schema_name"
lazy="true|false"
inverse="true|false"
cascade="all|none|save-update|delete|all-delete-orphan"
sort="unsorted|natural|comparatorClass"
order-by="column_name asc|desc"
where="arbitrary sql where
condition"
fetch="join|select"
batch-size="N"
access="field|property|ClassName"
optimistic-lock="true|false"
>
<key
.... />
<map-key .... />
<element .... />
|
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)
(13)
|
</map> |
(1) |
name
the collection property name |
(2) |
table
(optional - defaults to property name)
the name of the collection table (not
used for one-to-many associations)
|
(3) |
schema
(optional) the name of a table schema
to override the schema declared on
the root element |
(4) |
lazy
(optional - defaults to true) enable
lazy initialization (not available
for arrays) |
(5) |
inverse
(optional - defaults to false) mark
this collection as the "inverse"
end of a bidirectional association
|
(6) |
cascade
(optional - defaults to none) enable
operations to cascade to child entities
|
(7) |
sort
(optional) specify a sorted collection
with natural sort order, or a given
comparator class |
(8) |
order-by
(optional, JDK1.4 only) specify a
table column (or columns) that define
the iteration order of the Map, Set
or bag, together with an optional
asc or desc |
(9) |
where
(optional) specify an arbitrary SQL
WHERE condition to be used when retrieving
or removing the collection (useful
if the collection should contain only
a subset of the available data) |
(10) |
fetch
(optional, defaults to select) Choose
between outer-join fetching and fetching
by sequential select. Only one collection
may be fetched by outer join per SQL
SELECT. |
(11) |
batch-size
(optional, defaults to 1) specify
a "batch size" for lazily
fetching instances of this collection.
|
(12) |
access
(optional - defaults to property):
The strategy Hibernate should use
for accessing the property value.
|
(13) |
optimistic-lock
(optional - defaults to true): Species
that changes to the state of the collection
results in increment of the owning
entity's version. (For one to many
associations, it is often reasonable
to disable this setting.) |
These are some more mapings which we will
discuss later
Association Mapping
Component Mapping
Instance Mapping
|
|
|