This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Querying Inside Entities Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Reply Bookmark "Querying Inside Entities" Watch "Querying Inside Entities" New topic
Author

Querying Inside Entities

nitin pai
Ranch Hand

Joined: May 30, 2006
Posts: 185
Is there a way in which I can query a table inside the method of an entity.
I wanted to try out a scenario where in, if table B data has to be inserted then first check whether a column of table A has been set or not. If set then, insert into table B or else throw exception.

I know this can be done in a session bean easily, but I would like to know if this is possible inside the @PrePersist method of the Entity corresponding to table B itself.

Also, for what purpose are Entity Listeners used? Can they be useful in such cases?



nitin pai
Ranch Hand

Joined: May 30, 2006
Posts: 185
umm... anyone have a clue?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 8142
    
  52

Originally posted by nitin pai:

I wanted to try out a scenario where in, if table B data has to be inserted then first check whether a column of table A has been set or not. If set then, insert into table B or else throw exception.

I would like to know if this is possible inside the @PrePersist method of the Entity corresponding to table B itself.



Are those 2 entities (A and B) related to each other? If yes, then in the @PrePersist callback method of B, you could traverse to A and check the attributes of A. If they don't satisfy the requisite condition then you can throw the exception.


[My Blog] [JavaRanch Journal]
nitin pai
Ranch Hand

Joined: May 30, 2006
Posts: 185
Thanks Jai,

Yes the two tables are related. But how to traverse back. Could you please provide me an example.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 8142
    
  52

Originally posted by nitin pai:


Yes the two tables are related. But how to traverse back. Could you please provide me an example.


I was thinking along these lines:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Querying Inside Entities
 
Similar Threads
Mapping Question
how to handle foriegn key in entity design?
dependent entity beans - calling another entity bean's remove method
ejbActivate() and ejbPassivate() methods are called million times!!! :(
question about Enity mapping