이 콘텐츠는 선택한 언어로 제공되지 않습니다.
1.2.3. Working the association
Now we will bring some people and events together in a new method in
EventManager
:
After loading a
Person
and an Event
, simply modify the collection using the normal collection methods. There is no explicit call to update()
or save()
; Hibernate automatically detects that the collection has been modified and needs to be updated. This is called automatic dirty checking. You can also try it by modifying the name or the date property of any of your objects. As long as they are in persistent state, that is, bound to a particular Hibernate org.hibernate.Session
, Hibernate monitors any changes and executes SQL in a write-behind fashion. The process of synchronizing the memory state with the database, usually only at the end of a unit of work, is called flushing. In our code, the unit of work ends with a commit, or rollback, of the database transaction.
You can load person and event in different units of work. Or you can modify an object outside of a
org.hibernate.Session
, when it is not in persistent state (if it was persistent before, this state is called detached). You can even modify a collection when it is detached:
The call to
update
makes a detached object persistent again by binding it to a new unit of work, so any modifications you made to it while detached can be saved to the database. This includes any modifications (additions/deletions) you made to a collection of that entity object.
This is not much use in our example, but it is an important concept you can incorporate into your own application. Complete this exercise by adding a new action to the main method of the
EventManager
and call it from the command line. If you need the identifiers of a person and an event - the save()
method returns it (you might have to modify some of the previous methods to return that identifier):
This is an example of an association between two equally important classes : two entities. As mentioned earlier, there are other classes and types in a typical model, usually "less important". Some you have already seen, like an
int
or a java.lang.String
. We call these classes value types, and their instances depend on a particular entity. Instances of these types do not have their own identity, nor are they shared between entities. Two persons do not reference the same firstname
object, even if they have the same first name. Value types cannot only be found in the JDK , but you can also write dependent classes yourself such as an Address
or MonetaryAmount
class. In fact, in a Hibernate application all JDK classes are considered value types.
You can also design a collection of value types. This is conceptually different from a collection of references to other entities, but looks almost the same in Java.