4 Replies Latest reply on Oct 4, 2008 5:07 AM by Andy Gibson

    EntityHome.find after persist entity

    marcelo oikawa Newbie

      I have an EntityHome class called Subscription. This Home has two methods: method-A and method-B. The method-A persists an entity and before returning the persisted String I call the method-B. Inside method-B I need to load the instance again because some fields are filled by one database trigger, but when I try to load the instance, the Home (using find method) did not execute a search and can't get the values filled by the trigger.

      Someone has any idea of what is happening?

      Does anyone know how can I solve this?

        • 1. Re: EntityHome.find after persist entity
          Val Sw Expert

          Hi Marcelo,

          Did you found any solution I need to make JDBC call immediately AFTER persist to insert a row in some table.

          Is there any mothod which I can override... or any other option...

          • 2. Re: EntityHome.find after persist entity
            Andy Gibson Novice

            One of two options.

            Find will just re-use the instance the the entity manager already has locally without going back to the database. You probably want to call entityManager.refresh(getInstance()) in the entity home instead.

            Alternatively, if the property is immutable, I think you can use the @Generated hibernate annotation which will mark the property as immutable and also perform an automatic refresh which it is persisted.


            Andy Gibson

            • 3. Re: EntityHome.find after persist entity
              Val Sw Expert
              Thanks for replying Andy...

              I just need to call a function "insertQuantReagent();", once the values are commited to database.

              Will entityManager.refresh(getInstance()) commit the database before calling my function... which will be the best place to add it...

              public String persist(){
                   String persistResult = super.persist();
                   insertQuantReagent();//<--- CALLING IT
                   return persistResult;

              public void insertQuantReagent(){     
                   Utils u = new Utils();
                   try {
                   } catch (SQLException e) {
                        // TODO Auto-generated catch block
                   }// But this should be called after persist() method
              • 4. Re: EntityHome.find after persist entity
                Andy Gibson Novice

                Do you really only want to insert the rows if the item has been commited, or just when it has been fliushed  to the database and not committed?  What if your insert fails and you have the entity committed but without the additional reagent inserted?

                The persist method will flush it to the database, but it won't commit until the method has finished. However flushing is good enough as it makes it queryable since it is all taking place within the same transaction.

                In the code you wrote, I believe the two inserts will take place in the same transaction, and will both commit or rollback together.

                If this is related to your other post with one to one and one to many mappings, you can let JPA handle all that with the right relational mappings and cascades defined.