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:

      public class Data implements Serializable {
       // field declarations here (plus getters and setters)

      Bean code:
      public class ImportService implements Serializable {
       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
       // I hoped the data would be saved by now...

      Also I have this in components.xml:
       <core:managed-persistence-context name="entityManager"

      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"
       <persistence-unit name="MyDBUnit">
       <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"/>

      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.


                public class ContentBean implements ContentInterface
                 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);
                 String result = (String) query.getSingleResult();
                 System.out.println("id = " + id);
                 return result;
                 }catch(Exception e){
                 return "<NULL>";


                public class HtmlContent implements Serializable{
                 private static final long serialVersionUID = 1L;
                 private long id;
                 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;

                <?xml version="1.0" encoding="UTF-8"?>
                 <persistence-unit name="ponte">
                 <property name="hibernate.hbm2ddl.auto" value="none" />
                 <property name="hibernate.dialect"
                 value="org.hibernate.dialect.MySQLInnoDBDialect" />

                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