| 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
|
|
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
|
|
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:
|
 |
 |
|
|
subject: Querying Inside Entities
|
|
|