2 Replies Latest reply on Oct 21, 2006 10:18 AM by laszlo.fogas

    Database triggers and entity beans

    sbivol

      Hi,

      We're trying to get rid of few triggers in the database, but are struggling with finding a solution in the ejb3 land. The triggers we're struggling in particular are "after insert" triggers that update other record fields or insert records in same or other tables.

      First, it took us a while to figure out why within the same transaction the entities affected by the trigger would not reflect the data in the database.

      Second, we wanted to replace the triggers with java code, and @PostPersist EntityListeners or Callbacks seemed perfect for this, where we thought we could create new entities or find existing ones that need updating, and then use entity manager to persist data. However, we could not find a way to get a handle to the EntityManager.

      What is the correct way of doing this?

      Also, in a @PostUpdate or @PreUpdate, is there a way to find old AND new values?

      Thanks & Regards

        • 1. Re: Database triggers and entity beans
          busitech

          Please let us know if you find a nice way to do this. This limitation of not being able to use the entity manager during callbacks is really a problem!

          We are trying to find a clean way to set up simple calculated properties of an object which are assigned a value automatically when it is loaded, such as the sum total of one column, or even a count, from objects on the other side of a OneToMany association.

          A concrete example would be to add a YTDSales property to a Salesman object, or an ItemCount property to a Manufacturer object.

          There is a @Formula annotation which can contain an SQL string, and the docs state that a subselect can even be performed. However, we would really prefer to stay in Java working with objects!

          • 2. Re: Database triggers and entity beans
            laszlo.fogas

            hi,

            in an other topic i asked amost the same.. others offered me to use @resource annotation, or sg like that, but before that i do a simple jndi lookup to get the entitymanager. You have to put some properties into the persistence.xml jndiname or sg, and after you can get the manager. Please take care that your transaction is works perfectly! I tested it only once, and when i throw an exception during the callback method, it seems that stoped the transaction.

            lazlo