0 Replies Latest reply on Mar 19, 2007 7:40 PM by statelessbean

    EntityManager Questions

    statelessbean

      First sorry for this topic here on Seam forum but EJB forum seams to be dead.
      I'm newbie in EJB3.0, and need help.
      Can anyone help me to configure well my EntityManager and answer for my questions?
      My target is to get EntityManager working with persistence.xml configuration.
      and....

      I read examples and still have problem.

      I want to also use Seam Realm, and declared in component.xml have declared em.

      <component name="em" auto-create="true" class="org.jboss.seam.core.ManagedPersistenceContext">
       <property name="persistenceUnitJndiName">java:/sguEntityManagerFactory</property>
      </component>
      


      here is my persistence.xml:
      <persistence-unit name="Universum">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/MySqlDS</jta-data-source>
       <properties>
       <property name="dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
       <!-- <property name="hibernate.show_sql" value="true"/> -->
       <!-- <property name="hibernate.hbm2ddl.auto" value="create"/> -->
       <property name="jboss.entity.manager.factory.jndi.name" value="java:/sguEntityManagerFactory" />
       </properties>
       </persistence-unit>
      </persistence>
      


      and here i got problem:
      What's the difference beetwen:
      @PersistenceContext(unitName="Universum")
      EntityManager em;
      
      @Resource (mappedName="java:/sguEntityManagerFactory")
      EntityManager em;
      


      Should I also declare EntityManagerFactory in presistence.xml?


      What's the difference beetwen:
      @PersistenceUnit
      
      @PersistenceContext
      


      In some examples i see people do:
      EntityTransaction tx=em.getTransaction();
      
      try {
       tx.begin();
      
       // Do saves or modifications here
      
       tx.commit();
      } catch (Exception e) {
       if(tx.isActive()) tx.rollback();
       System.err.println("Error:"+e);
      } finally {
       em.close();
      }
      


      and in some examples don't, for what is that? Is that mean that transaction is started?

      Why when i use
      @PersistenceContext
      EntityManager em;
      


      and do
      EntityTransaction tx=em.getTransaction();
      

      i got following error:
      "JTA EntityManager cannot access a transactions"

      but if I do
      @PersistenceContext(unitName="Universum")
      protected EntityManager em;
      


      I get "Illegal to call this method from injected, managed EntityManager"