2012-02-20 72 views
0

我有一些自動生成的Hibernate DAO代碼,它是由Eclipse-hibernate逆向工程插件生成的。休眠自動生成的代碼報告成功堅持,但沒有新的記錄發現數據庫

EntityManager對象的'persist'方法不會引發任何異常,但該對象不會持久保存到數據庫。目前數據庫的表是空的(沒有記錄)。

我得到以下輸出,有什麼想法?

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 

同樣,這裏是一個代碼剪斷,我手動創建一個「小寵物的對象,並將其傳遞到「持續(..)」方法在這裏。

@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; 
    } 
} 

回答

1

em.commit()

由於性能原因,當你調用em.persist(),Hibernate持久化對象的基本模式,但在你提交之前不會實際執行數據庫指令交易。

+0

問題解決! (通過包裝我的方法是下面的語句)\t entityManager.getTransaction()。begin(); entityManager.getTransaction()。commit(); – 2012-02-20 22:45:08

+1

如果你使用Spring,你可以註釋你的@Transactional方法。 – JMelnik 2012-02-20 22:46:35