• Post Reply Bookmark Topic Watch Topic
  • New Topic

Querying Inside Entities

 
nitin pai
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
umm... anyone have a clue?
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nitin pai
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jai,

Yes the two tables are related. But how to traverse back. Could you please provide me an example.
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!