Java Reference
In-Depth Information
Retrieved...
Test Email
null
Test Message
null
As you can see, although the two entities have been saved, the attempt to associate the
Email with the Message entity by calling a method on the Message entity has not been honored
in the database, because the Message entity does not own the association. The following list
recaps the points made so far:
You must explicitly manage both ends of an association.
Only changes to the owner of an association will be honored in the database.
When you load a detached entity from the database, it will reflect the foreign key rela-
tionships persisted into the database.
Table 4-1 shows how you can select the side of the relationship that should be made the
owner of a bidirectional association. Remember that to make an association the owner, you
must mark the other end as inverse="true" (the choice of terminology is poor, but
entrenched).
Table 4-1. Marking the Owner of an Association
Type of Association
Options
One-to-one
Either end can be made the owner, but one (and only one) of them should
be—if you don't specify this, you will end up with a circular dependency.
One-to-many
The many end must be made the owner of the association.
Many-to-one
This is the same as the one-to-many relationship, viewed from the opposite
perspective, so the same rule applies—the many end must be made the
owner of the association.
Many-to-many
Either end of the association can be made the owner.
If this all seems rather confusing, just remember that association ownership is concerned
exclusively with the management of the foreign keys in the database, and things should
become clearer as you use Hibernate further. Associations and mappings are discussed in
detail in the next three chapters.
Saving Entities
Creating an instance of a class you mapped with a Hibernate mapping does not automatically
persist the object to the database. Until you explicitly save the object with a valid Hibernate ses-
sion, the object is transient, like any other Java object. In Hibernate, we use one of the save()
methods on the Session interface to store a transient object in the database, as follows:
public Serializable save(Object object) throws HibernateException
public void save(Object object, Serializable id) throws HibernateException
public Serializable save(String entityName,Object object) throws HibernateException
Search WWH ::




Custom Search