Java Reference
In-Depth Information
Session session = factory.openSession();
session.beginTransaction();
Email email = new Email("Email title");
Message message = new Message("Message content");
email.setMessage(message);
message.setEmail(email);
session.save(email);
session.getTransaction().commit();
session.close();
session = factory.openSession();
session.beginTransaction();
Ideally, we would like the save operation to be propagated from the
Email
entity to its
associated
Message
object. We do this by setting the cascade operations for the properties and
fields of the entity (or assigning an appropriate default value for the entity as a whole). So, the
preceding code will perform correctly if at least the save cascade operation is set for the
Email
entity's
message
property. All of the basic life cycle operations discussed in this chapter have
associated cascade values, as follows:
•
create
•
merge
•
delete
•
save-update
•
evict
•
replicate
•
lock
•
refresh
These values can be concatenated in a comma-separated list to allow cascading for any
combination of these operations. When all operations should be cascaded, Hibernate provides
a shortcut value named
all
that tells Hibernate to cascade all of these operations from the
parent to each child object (for that relationship).
As part of the Hibernate mapping process, you can tell Hibernate to use one of these
cascading types for a relationship between two objects (the parent and the child). On the
collection
or
property
element in the mapping file, set the
cascade
attribute to the type (or
types) you would like to use.
By default, Hibernate does not cascade any operations—the default behavior can be over-
ridden at the entity level via the XML mapping files using the
default-cascade
attribute on the
<hibernate-mapping>
XML element or in the annotated source files.
Search WWH ::
Custom Search