4 Replies Latest reply on Apr 4, 2007 8:19 AM by Manu Osten

    entityManager.persist(Object) not working?

    eric es Newbie

      Hello all, I have a small question I don't know if the Seam forum is the right place to post, but I use a seam managed entity manager and it's behaviour puzzles me... A very simplified sitation sketch:

      data class:

      @Entity()
      @Name("data")
      public class Data implements Serializable {
       // field declarations here (plus getters and setters)
      }
      


      Bean code:
      @Name("importService")
      @Scope(ScopeType.CONVERSATION)
      public class ImportService implements Serializable {
       @In()
       private EntityManager entityManager;
      
       public void parse(Document d) {
       Data data = new Data();
       // Document d is an XML file, wich is parsed, all values are set in
       // the data object
      
       this.entityManager.persist(data);
       // I hoped the data would be saved by now...
       }
      }
      

      Also I have this in components.xml:
       <core:managed-persistence-context name="entityManager"
       auto-create="true"
       persistence-unit-jndi-name="java:/MyEntityManagerFactory"/>
      

      And persistence.xml:
      <?xml version="1.0" encoding="UTF-8"?>
      <!-- Persistence deployment descriptor for dev profile -->
      <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
      http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
       <persistence-unit name="MyDBUnit">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/MyDatasource</jta-data-source>
       <properties>
       <property name="hibernate.hbm2ddl.auto" value="update"/>
       <property name="hibernate.cache.use_query_cache" value="true"/>
       <property name="hibernate.show_sql" value="true"/>
       <property name="jboss.entity.manager.factory.jndi.name" value="java:/MyEntityManagerFactory"/>
       </properties>
       </persistence-unit>
      </persistence>
      

      On deploy time, the database table Data is created perfectly, I get all the SQL statements on my console log. No problem. If I manually insert data, I can query for the data using this.entityManager.createQuery("from Data data"); even INSIDE the parse method of the bean, again I see the SQL statement in my console log. But on the entityManager.persist(data) call, there's no SQL statement executed. I surrounded the call with a try { ... } catch (Exception e) { ... } block but there are NO exceptions thrown. Just no data is saved :(

      Can someone help me with this please? I really don't understand why data is not saved...!

        • 1. Re: entityManager.persist(Object) not working?
          Gavin King Master

          You need to have a transaction active if you want persist() to write to the database.

          • 2. Re: entityManager.persist(Object) not working?
            eric es Newbie

            Thanks very much, I've read some more things on this subject last night because I see I don't really have the knowledge yet... Indeed I needed a transaction, I solved this by annotating the method with @Transactional(), it works perfectly now!

            • 3. Re: entityManager.persist(Object) not working?
              Gavin King Master

              Its more correct to have a single transaction spanning all your persistence operations. You might be better served using a Seam-managed transaction.

              • 4. Re: entityManager.persist(Object) not working?
                Manu Osten Apprentice

                I think I have a similar problem like that post. That's why, I join that post.

                Suse 10.2 JBOSS 4.0.5.GA EJB 3.0

                I tried to get a data from mysql using manager.createQuery(); But entityManager is not working.

                Bean

                @Stateless
                public class ContentBean implements ContentInterface
                {
                 @PersistenceContext(unitName="ponte")
                 EntityManager manager;
                
                 public String getContent(long id) {
                 Query query = manager.createQuery(
                 "select content.content from HTML_CONTENT content where content.id = :id");
                 query.setParameter("id", id);
                 try{
                 String result = (String) query.getSingleResult();
                 System.out.println("id = " + id);
                 System.out.println("result");
                 return result;
                 }catch(Exception e){
                 System.out.println("Error");
                 return "<NULL>";
                 }
                 }
                }
                


                Data

                @Entity
                @Table(name="HTML_CONTENT")
                public class HtmlContent implements Serializable{
                
                 private static final long serialVersionUID = 1L;
                
                 @Id
                 @Column(name="ID")
                 private long id;
                
                 @Column(name="CONTENT")
                 private String content;
                
                 public void setId(long id) {
                 this.id = id;
                 }
                
                 public long getId() {
                 return id;
                 }
                
                 public void setContent(String content) {
                 this.content = content;
                 }
                
                 public String getContent() {
                 return content;
                 }
                }
                


                persistence.xml
                <?xml version="1.0" encoding="UTF-8"?>
                
                <persistence>
                 <persistence-unit name="ponte">
                 <jta-data-source>java:/MySqlDS</jta-data-source>
                 <properties>
                 <property name="hibernate.hbm2ddl.auto" value="none" />
                 <property name="hibernate.dialect"
                 value="org.hibernate.dialect.MySQLInnoDBDialect" />
                 </properties>
                 </persistence-unit>
                </persistence>
                


                I tried to add @TransactionAttribut annotation, but it is still not working.

                Konsole Output after invoking this methode getContent() :

                13:39:47,670 INFO [STDOUT] [Ljava.lang.StackTraceElement;@503cb2


                Can someone tell me, what is wrong, or? Thanks

                regards
                Manu