my dog learned polymorphism*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Multiple Entity callback methods in entity hierarchy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Multiple Entity callback methods in entity hierarchy" Watch "Multiple Entity callback methods in entity hierarchy" New topic
Author

Multiple Entity callback methods in entity hierarchy

srikanth pragada
Greenhorn

Joined: Nov 08, 2006
Posts: 7
Hi Everyone,

I have created the following code.



The application is deployed successfully. I am using Glassfish and TopLink.
But when an Employee entity is loaded using find() method, it is causing error which says that multiple methods are present for the same event.

But SPECS say the following in Section 3.5.4.

If a lifecycle callback method for the same lifecycle event is also specified on the entity class and/or one
or more of its entity or mapped superclasses, the callback methods on the entity class and/or superclasses
are invoked after the other lifecycle callback methods, most general superclass first. A class is
permitted to override an inherited callback method of the same callback type, and in this case, the overridden
method is not invoked.


I have tried to override the same method. It is calling the method in subclass ( correctly).

Can anyone explain what's wrong with the above code.

Srikanth.


SCJP 5.0 (97%), SCWCD 1.4 (92%), SCBCD 5.0 (96%)
Anton Golovin
Ranch Hand

Joined: Jul 02, 2004
Posts: 476
If a lifecycle callback method for the same lifecycle event is also specified on the entity class and/or one
or more of its entity or mapped superclasses, the callback methods on the entity class and/or superclasses
are invoked after the other lifecycle callback methods, most general superclass first. A class is
permitted to override an inherited callback method of the same callback type, and in this case, the overridden
method is not invoked.

This is a tricky wording: nowhere does it say the same lifecycle event could be specified several times in a hierarchy. You could, however, override an inherited callback method, but i think overriding in this case literally means the same method name.
[ November 04, 2008: Message edited by: Anton Golovin ]

Anton Golovin (anton.golovin@gmail.com) SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
Tomaszz Lewandowski
Ranch Hand

Joined: Sep 07, 2007
Posts: 30
Hello,

I don't agree with this, because of the sentence:

"If a lifecycle callback method for the same lifecycle event is also specified on the entity class and/or one
or more of its entity or mapped superclasses, the callback methods on the entity class and/or superclasses
are invoked after the other lifecycle callback methods, most general superclass first."

I think that this sentence describes exactly above situation and it assumes that the same lifecycle event could be specified several times in a hierarchy.

Am I wrong?


Regards<br />Tomasz Lewandowski<br />SCJP5 (97%) | SCWCD5 (98%)
srikanth pragada
Greenhorn

Joined: Nov 08, 2006
Posts: 7
Hello,

Yes. What I Understand from the SPECS is that we can have a method in superclass as well as subclass for the same event. But that is NOT the case with TopLink.

Who is wrong? SEPCS cannot be wrong. Is TOPLINK not doing what it should?

I am preparing for SCBCD 5.0. These inconsistensies could come in my way and take a question or two from me in the exam. I am worried about that.

Can idea about what stand should we take?

Thanks,
Srikanth.
Anton Golovin
Ranch Hand

Joined: Jul 02, 2004
Posts: 476
Then for portability reasons, you might want to simplify your callbacks. Generally speaking, the amount of bugs in persistence providers is terrible. They should make a qualitative assessment of the persistence providers the first question on that exam.

Did you manage to rename your method and see what TopLink thought about it?
[ November 05, 2008: Message edited by: Anton Golovin ]
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Did you manage to rename your method and see what TopLink thought about it?


In toplink, if I have the same named postload callback in the entity and its mapped superclass, the callback in made twice to the postlaod callback method of the entity. On the other hand, as already stated, for different named callback methods, an exception is throw saying that there cannot be multiple call back methods for the same event. Is this a bg with toplink?


Thanks and Regards
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

This is what I have with Oracle TopLink Essentials 2.0.1 :

  • Overriding postLoad : "Earth loaded" is printed. Works as excected.
  • Renaming Earth#postLoad to Earth#postLoading (thus not overriding) : deployment fails : The listener class [class com.webage.ejbs.mappedsuperclass.Earth] has multiple lifecycle callback methods for the same lifecycle event ([public void com.webage.ejbs.mappedsuperclass.Planet.postLoad()] and [public void com.webage.ejbs.mappedsuperclass.Earth.postLoading()]).

  • Mmmh, as Srikanth Praga says, that's not what I would expect.

    The closest I've found is this bug, but it is still a bit different from what we have here.


    [My Blog]
    All roads lead to JavaRanch
    Christophe Verré
    Sheriff

    Joined: Nov 24, 2005
    Posts: 14687
        
      16

    It's puzzling me, so I tried with Hibernate instead of Toplink. Guess what :

  • Overriding postLoad : "Earth loaded" is printed. Works as expected.
  • Renaming Earth#postLoad to Earth#postLoading (thus not overriding) : "Planet loaded", "Earth loaded" is printed. Works as expected


  • It looks like a Toplink bug.
    Anton Golovin
    Ranch Hand

    Joined: Jul 02, 2004
    Posts: 476
    All this means is that the developer teams took different meanings to the word override in the spec.
    Christophe Verré
    Sheriff

    Joined: Nov 24, 2005
    Posts: 14687
        
      16

    No, the problem here is not about the overriding part, as it's working fine. The problem is in the first part:
    If a lifecycle callback method for the same lifecycle event is also specified on the entity class and/or one or more of its entity or mapped superclasses, the callback methods on the entity class and/or superclasses are invoked after the other lifecycle callback methods, most general superclass first.
    Anton Golovin
    Ranch Hand

    Joined: Jul 02, 2004
    Posts: 476
    Why don't the bartenders post a set of links to free online resources?

    [ November 07, 2008: Message edited by: Anton Golovin ]
    [ November 07, 2008: Message edited by: Ulf Dittmer ]
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 41598
        
      55
    Why don't the bartenders post a set of links to free online resources?

    I'm assuming you're asking about the SCBCD, since that's what this forum is about. Such a page exists already: http://faq.javaranch.com/java/ScbcdLinks Being a wiki page, everyone can contribute to it.


    Ping & DNS - my free Android networking tools app
    David O'Meara
    Rancher

    Joined: Mar 06, 2001
    Posts: 13459

    Are you aware that we have a wiki where all members are able to submit their own information?

    We already have a huge number of categories, you may want to look at the SCJP FAQ as an example of the resources already available.

    [Dave - removed some duplicate information]
    [ November 07, 2008: Message edited by: David O'Meara ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Multiple Entity callback methods in entity hierarchy