1

i have some trouble with hibernate. The EntityManager.merge() does not have any effect, neither does persist().

My code:

@Stateless
public class UserManager {

@Inject
private EntityManager em;

public User save(User user){
    return em.merge(user);
}
}

The User entity is pretty simple:

@Entity
public class User implements Serializable {

private static final long serialVersionUID = -6249050000984099316L;

@Id
@GeneratedValue
private Long id;

private String name;

//some more fields
//...

// getters / setters following
//...
}

My ManagedBean is just calling userManager.save(user) and passing a user-object.

The User-Table in my database is completely empty, so i expect merge() to result in an INSERT statement. There is neither an insert nor an update on table User. Only the next_val for the generated ID is updated:

INFO  [stdout] (http-localhost-127.0.0.1-8082-7) Hibernate: select next_val as id_val from hibernate_sequence for update
INFO  [stdout] (http-localhost-127.0.0.1-8082-7) Hibernate: update hibernate_sequence set next_val= ? where next_val=?

Please help me. Thanks in advance.

EDIT:

EntityManager Producer:

public class Resources {

   @PersistenceContext
   private EntityManager em;

   @Produces
   public EntityManager produceEntityManager()
   {
      return em;
   }
}

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
    <persistence-unit name="primary" transaction-type="RESOURCE_LOCAL">
        <jta-data-source>java:jboss/datasources/dkmgr</jta-data-source>
        <properties>
            <!-- Properties for Hibernate -->
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.import_files_sql_extractor"
                value="org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor" />
            <property name="hibernate.hbm2ddl.import_files" value="import.sql" />
        </properties>
    </persistence-unit>
</persistence>
4

1 回答 1

2

If you are using EJB maybe you should try this:

public User save(User user){
    em.getTransaction().begin();
    em.merge(user);
    em.getTransaction().commit();
    return user;
}
于 2013-09-08T12:54:13.507 回答