1 Reply Latest reply on Nov 1, 2011 12:18 AM by Jason Porter

    SEAM 3 PERSISTENCE JPA JTA HIBERNATE

    tehackio teha Newbie
      I'm using seam 3 persistence, hibernate 4xx, JTA, JBOSS as 7, weld, richfaces 4.0.Final

      My problem is: detached entity passed to persist;
      It happens because each method executes in transaction, so, causes persistence context has been detached.
      I wanna call "public void setCurrentId(String id){", set instance by entitymanager find, edit on my jsf2.0 form, and then, persit to the database.

      My relevant code:

      @Named("myHome")
      @ViewScoped
      public class MyHome extends AbstractHome<fooEntity>{
      ..

      @Inject
      private DAO<fooEntity> dao;

      public void setCurrentId(String id){
        getDao().newInstance();
        getDao().setCurrentId(id);
        getDao().setInstance((fooEntity) getEntityManager().find(fooEntity.class, getDao().getId()));
      }
      ..
      }//end myHome

      public abstract class AbstractHome<T> implements Serializable {
      private static final long serialVersionUID = 1L;

        @PersistenceContext(type=PersistenceContextType.EXTENDED)
        private EntityManager entityManager;
      ...
        public EntityManager getEntityManager() {
          return this.entityManager;
        }
      ...
      }//end AbstractHome

      public class DAOFactory<T> implements Serializable {
      private static final long serialVersionUID = 1L;

        @ExtensionManaged
        @PersistenceContext(type=PersistenceContextType.TRANSACTION)
        private EntityManager em;

        @Produces
        public DAO<T> create(InjectionPoint injectionPoint) {
          ParameterizedType type = (ParameterizedType) injectionPoint.getType();
          Class classe = (Class) type.getActualTypeArguments()[0];
          DAO<T> dao = new DAO<T>(classe, this.em);
          return dao;
        }

      }//end DAOFactory

      public class DAO<T>
      ...
      public void save() {
        getEntityManager().joinTransaction();
        getEntityManager().persist(getInstance());
      }
      ..
      }//end DAO<T>