0 Replies Latest reply on Aug 22, 2006 12:11 PM by Jonathan Beer

    Database triggers

    Jonathan Beer Newbie


      I am storing a heirarchical structure in the database using left and right traversal values. Every time I insert a record into a table, I have a after-insert trigger than fires and updates many records in this table (a number of rows left and right traversal values can change when inserting a new record).

      This works fine. Enter EJB3. Now this doesn't work fine: When I persist an entity that affects mutliple rows in the database, I need to refresh all of the entities that were affected by the after-insert trigger in the database. I don't know the exact entities that will be affected, so it seems like clearing the entity manager might be a good way (albeit expensive) to accomplish this. Entity callbacks seemed like the way to approach this, using @PostPersist annotation on the entities that get updated. However, I can't seem to get a hold of the EntityManager (through the bean, injection, or a JNDI lookup).

      a) Can you get an EntityManager from an entity callback?

      b) Is there a better approach to do this? I have thought of moving the database triggers into java code in the Entity callback, but since I can't get the EntityManager at this point, that's not a viable option.

      Any thoughts and help is appreciated,