Question

I have some auto-generated hibernate DAO code which was generated by Eclipse-hibernate reverse engineering plugin.

The EntityManager object's 'persist' method is not throwing any exception but the object is not persisted to the database. Currently the database is table is empty (no records).

I get the following output, any ideas?

Starting test 'createNewAccountTest'
Feb 20, 2012 5:25:47 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Feb 20, 2012 5:25:47 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.0.1.Final}
Feb 20, 2012 5:25:47 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.username=root, hibernate.connection.password=****, hibernate.dialect=org.hibernate.dialect.MySQLDialect, hibernate.connection.url=jdbc:mysql://localhost/ptbrowserdb, hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.driver_class=com.mysql.jdbc.Driver}
Feb 20, 2012 5:25:47 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Feb 20, 2012 5:25:48 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Feb 20, 2012 5:25:48 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
Feb 20, 2012 5:25:48 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: true
Feb 20, 2012 5:25:48 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/ptbrowserdb]
Feb 20, 2012 5:25:48 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****, autocommit=true, release_mode=auto}
Feb 20, 2012 5:25:48 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Feb 20, 2012 5:25:48 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
Feb 20, 2012 5:25:48 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
Feb 20, 2012 5:25:48 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
persist successful

As well, here is a code snip, I am manually creating a 'Pokemons' object and passing it into the 'persist(..)' method here.

@PersistenceContext private EntityManager entityManager;

private void initManager()
{
    if(entityManager != null)
        return;

    EntityManagerFactory factory;
    factory = Persistence.createEntityManagerFactory("manager1");
    entityManager = factory.createEntityManager();
}

public void persist(Pokemons transientInstance) {
    if(entityManager == null)
        initManager();


    log.debug("persisting Pokemons instance");
    try {
        entityManager.persist(transientInstance);
        System.out.println("persist successful");
        log.debug("persist successful");
    }
    catch (RuntimeException re) {
        log.error("persist failed", re);
        throw re;
    }
}
Was it helpful?

Solution

em.commit()

For performance reasons, when you call em.persist(), Hibernate persists object to the underlying model, but does not actually execute database instruction until you commit the transaction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top