Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ejbPostCreate()  RSS feed

 
mathew mathews
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • 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.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • 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():
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks lasse
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!