users@glassfish.java.net

PostLoad life cycle callbacks for Entities invoked twice

From: <glassfish_at_javadesktop.org>
Date: Tue, 27 Nov 2007 23:26:38 PST

I am using Toplink-Essentials to try out JPA. For the back-end I'm using Sybase. I've written two entities, [b]Employee[/b] and [b]Department[/b], with a [b]ManyToOne[/b] relationship defined on the [b]Employee[/b] entity with cascade type set to [b]PERSIST[/b]. In both the classes I've setup [b]@PrePersist[/b] and [b]@PostLoad[/b] callback methods for simple logging purposes where I'm just printing the current date time and the action name (Persist/Load) followed by the entity name. A sample output when persisting an [b]Employee[/b] looks like the following:
****************************************************
[TopLink Info]: 2007.11.28 12:33:48.700--ServerSession(23163273)--TopLink, version: Oracle TopLink Essentials - 2006.4 (Build
 060412)
[TopLink Info]: 2007.11.28 12:34:25.361--ServerSession(23163273)--file:/C:/Documents%20and%20Settings/131893/My%20Documents/1
31893/Java%20Programs/Toplink-EmployeeService login successful
Wed Nov 28 12:34:25 IST 2007 - Persisting Employee...
Wed Nov 28 12:34:25 IST 2007 - Persisting Department...
Employee Created: Id=67,Name=Nathan,Salary=9000,Dept=Sales
[TopLink Info]: 2007.11.28 12:34:25.532--ServerSession(23163273)--file:/C:/Documents%20and%20Settings/131893/My%20Documents/1
31893/Java%20Programs/Toplink-EmployeeService logout successful
****************************************************
Thoug this looks straightforward, things become a bit strange when I try to load an [b]Employee[/b]. The [b]@PostLoad[/b] gets triggered twice for both the [b]Employee[/b] and the [b]Department[/b].
****************************************************
[TopLink Info]: 2007.11.28 12:39:31.270--ServerSession(23163273)--TopLink, version: Oracle TopLink Essentials - 2006.4 (Build
 060412)
[TopLink Info]: 2007.11.28 12:40:08.008--ServerSession(23163273)--file:/C:/Documents%20and%20Settings/131893/My%20Documents/1
31893/Java%20Programs/Toplink-EmployeeService login successful
Wed Nov 28 12:40:08 IST 2007 - Loaded Department.
Wed Nov 28 12:40:08 IST 2007 - Loaded Employee.
Wed Nov 28 12:40:08 IST 2007 - Loaded Department.
Wed Nov 28 12:40:08 IST 2007 - Loaded Employee.
Employee Found: Id=61,Name=Jeremy,Salary=15000,Dept=Sales
[TopLink Info]: 2007.11.28 12:40:08.180--ServerSession(23163273)--file:/C:/Documents%20and%20Settings/131893/My%20Documents/1
31893/Java%20Programs/Toplink-EmployeeService logout successful
****************************************************
I'm trying all this in Java SE 5.0.

Can someone please explain why this is happening?
[Message sent by forum member 'iamsouravin' (iamsouravin)]

http://forums.java.net/jive/thread.jspa?messageID=247516