Java Reference
In-Depth Information
EJB container-managed persistence (CMP) does work in this way—when a reference is
assigned in one entity, the corresponding reference in the other entity will be updated to
reflect this. Hibernate does not take this approach because it was designed to work in other
environments and with POJOs from other bodies of code, where such behavior would be
unexpected. If you pass a pair of objects to some third-party API, mysterious side effects
should not occur. Since Hibernate is precisely that—a third-party API—from the perspective
of most client code, Hibernate cannot safely cause their references to become connected in
this way!
Though Hibernate does not produce this behavior automatically, there is a side effect of
persistence to the database that can make it appear that it does (see Listing 4-4).
Listing 4-4. Misleading Behavior
openSession();
beginTransaction();
Email email = new Email("Test Email");
Message message = new Message("Test Message");
email.setMessage(message);
save(email,message);
System.out.println("Stored...");
System.out.println(email);
System.out.println(email.getMessage());
System.out.println(message);
System.out.println(message.getEmail());
Serializable emailPrimaryKey = session.getIdentifier(email);
Serializable messagePrimaryKey = session.getIdentifier(message);
endTransaction();
closeSession();
System.out.println();
openSession();
beginTransaction();
email = (Email)session.get(Email.class,emailPrimaryKey);
message = (Message)session.get(Message.class,messagePrimaryKey);
Search WWH ::




Custom Search