5 Replies Latest reply on Aug 31, 2008 8:45 PM by rollinm

    EntityManager cannot persist

    yuriy_zubarev

      It's a pretty basic set-up. There are EntityHome and POJO classes. The first one raises an event and the second one consumes it. The entityManager in the POJO is good for finding entities but does absolutely nothing when I try to persist/update an entity (no log output whatsoever).


      public abstract class PartyHome<P extends Party> extends EntityHome<P> {
        @In
        private Events events;
      
        @Override
        public String update() {
          String result = super.update();
          this.events.raiseAsynchronousEvent("party.changed", getInstance().getId());
          return result;
        }
      }
      




      @Name("partyObserver")
      public class PartyObserver {
      
        @Logger
        private Log log;
      
        @In
        EntityManager entityManager;
      
        @Observer("party.changed")
        public void handleChangedParty(long partyId) {
          Party party = this.entityManager.find(Party.class, partyId); // this works
          ...
          this.entityManager.persist(client); // this doesn't !!!
        }
      }



      Any hints?

        • 1. Re: EntityManager cannot persist
          jazir1979

          The entity manager won't send anything to the DB until em.flush() is called, and none of it will be committed until the underlying transaction committed.


          What kind of environment are you running Seam in, and what's your transaction provider?

          • 2. Re: EntityManager cannot persist
            yuriy_zubarev

            Of course! Thank you.


            I'm running under Tomcat 6. Transaction-type is RESOURCE_LOCAL
            and provider is org.hibernate.ejb.HibernatePersistence. Everything is fine now:


            EntityTransaction t = this.entityManager.getTransaction();
            t.begin();
            this.entityManager.persist(client);
            t.commit();
            



            By the way, is there more concise way of doing it?

            • 3. Re: EntityManager cannot persist
              jazir1979

              Take a look at the section on seam managed transactions, I think this is probably what you need.  http://docs.jboss.com/seam/2.0.0.GA/reference/en/html_single/#persistence.seam-managed-transactions

              • 4. Re: EntityManager cannot persist
                rollinm

                I have been working with seam 2.0.2sp1 running in jboss4.0.2.


                I have a similar issue that was described here. I have an action that I want to trigger as an asynchronous method. The async method needs to update the database.



                I've tried using the hibernate session and the entitymanager to update the db, but in both cases, the updates or inserts are never commited to the db. If I change it to synchronous call everything works fine.



                db update bean


                @Name("updateScan")
                public class UpdateScan {
                 
                    @In
                    private EntityManager entityManager;
                    private boolean isRunning;
                
                
                 
                    @Observer("items.changed")
                    public void sessTest() {
                     Session s =   (Session) entityManager.getDelegate();
                     Mediavideoext mve = new Mediavideoext();
                     mve.setLanguage("hi");
                      Transaction tr = s.beginTransaction();
                     s.save(mve);
                        tr.commit();
                
                    }
                
                
                }
                



                Call from page bean:


                //this does not work
                
                Events.instance().raiseAsynchronousEvent("items.changed");
                
                //this will work
                
                Events.instance().raiseEvent("items.changed");




                If there's some examples  or pointers using asynchronous and db updates to a db from within a Seam app I would appreciate the references.



                Thanks

                • 5. Re: EntityManager cannot persist
                  rollinm

                  Correction running jboss 4.2.2GA