Java Reference
In-Depth Information
The access attribute specifies that the price is a field rather than a property.
Hibernate will read and write the price field, which can be private, directly with-
out calling an accessor.
You can avoid having to specify an access attribute for every property by defin-
ing the default in the <hibernate-mapping> element:
<hibernate-mapping
default-access="field">
<class name="MenuItem" …>
<property name="price" column="PRICE"/>
</class>
Because the default-access attribute has the value of field , the <property> ele-
ment refers to the field rather than the property.
Mapping properties is useful in some situations, such as when an object needs
to initialize some nonpersistent fields or transform the persisted value. However,
except in those rare situations I would recommend mapping fields. Since Hiber-
nate's purpose is to store the state of an object in the database schema, I have
found no benefit in hiding that state from Hibernate with accessors. Moreover,
many objects have getters for their state but do not define setters. For example,
the PendingOrder class has a getDeliveryAddress() method but does not define a
setDeliveryAddress() method. Its client must instead call updateDeliveryInfo() ,
which validates its arguments. There is little value in defining a private
setDeliveryAddress() method for Hibernate's exclusive use. Even though it is
generally considered to be bad practice to access an object's fields directly, this is a
situation where it is perfectly acceptable.
6.1.2
Hibernate entities and components
When we're using Hibernate, an important part of defining the O/R mapping for
a domain model is to determine which classes are entities and which are compo-
nents. This distinction is important because Hibernate maps entities and compo-
nents to the database in slightly different ways. A Hibernate entity is a standalone
object whose lifecycle is independent of the lifecycle of any other object that ref-
erences it. This is similar to but not quite the same as the domain model entity
concept you learned about in chapter 3.
In contrast, a Hibernate component is an object that is part of some other par-
ent object and that is persisted and deleted with its parent. A component is also
 
 
Search WWH ::




Custom Search