• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ejbPostCreate()

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As ejb specification says container will call ejbPostCreate() immediately after ejbCreate().what exactly the operation happening in ejbPostCreate() method.Please clarify it with a sample code.Because i put this point in many of the discussions and didn't get a convincing answer.anyone there to
help me..
Thanks in Advance
Mathews
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From EJB 2.0 spec:
The entity Bean Provider may use the ejbPostCreate<METHOD>(...) to set the values of cmr-fields to complete the initialization of the entity bean instance.
The entity object identity is available for ejbPostCreate() methods through EntityContext#getPrimaryKey() and thus the entity bean can pass a reference to itself (remember that 'this' is not a valid reference!) to another bean's constructor, for example.
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You make use of primarykey.getEJBObject to pass the reference.
 
mathew mathews
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lasse,can you please clarify in detail what do you mean by completing the initialization of the entity bean instance? its already been assigned by some values in ejbCreate() method itself right...?
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the bean is not considered created until the method completes.

Originally posted by mathew mathews:
Lasse,can you please clarify in detail what do you mean by completing the initialization of the entity bean instance? its already been assigned by some values in ejbCreate() method itself right...?

 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mathew mathews:
Lasse,can you please clarify in detail what do you mean by completing the initialization of the entity bean instance? its already been assigned by some values in ejbCreate() method itself right...?


The most common example is that you have a container-managed relationship with another bean. Let's say you have an entity bean called Car. Well, a Car needs an Engine so it needs to create one. However, the Engine needs to know the Car it is being installed into so that it can control it. Now the problem is that you can't call EngineHome.create(Car car) from Car.ejbCreate() because you have no access to getPrimaryKey() at that point (the EJB spec says so). The solution is to initialize everything you can in ejbCreate() but do the following in ejbPostCreate():
 
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess the fact that ejbCreate() has executed and the primary key is available (in ejbPostCreate()) does'nt mean that a corresponding row has been created in the database. Nothing has been commited until ejbPostCreate execution. I suppose this is the case with foreign keys with not null constraints.
My question is can I be sure that I will find a row created in the DB atleast after post create is through?, i mean what is the time of commit to the database. So many things are left hanging as options that sometimes it gets irritating. Just like say the finalize() method! :-)
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My question is can I be sure that I will find a row created in the DB atleast after post create is through?


I think the container says INSERT after ejbCreate() returns. The ejbPostCreate() is called within the same transaction as ejbCreate() so I guess the record should be visible for a SELECT made from that same transactional context.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lasse Koskela:

I think the container says INSERT after ejbCreate() returns. The ejbPostCreate() is called within the same transaction as ejbCreate() so I guess the record should be visible for a SELECT made from that same transactional context.


Oops. EJB 2.0 spec (chapter 10.5.3) says:
The container may create the representation of the entity in the database immediately, or it can defer it to a later time (for example to the time after the matching ejbPostCreate<METHOD>(...) has been called, or to the end of the transaction), depending on the caching strategy that it uses.
 
mathew mathews
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lasse,the code you wrote in ejbPostCreate()is relevant in EJB 2.0 where you have a CMR .But how will you justify the point in EJB 1.1.Please clarify
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But how will you justify the point in EJB 1.1


Well, in EJB 1.1 you could still have the need to pass a reference to this entity bean to someone else. I can't think of a good example but I'm sure there are cases where this needs to be done.
 
mathew mathews
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks lasse
 
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic