【问题标题】:Hibernate auto-generated code reporting successful persist but no new records in DB foundHibernate 自动生成的代码报告成功,但在 DB 中没有找到新记录
【发布时间】:2012-02-20 22:30:27
【问题描述】:

我有一些由 Eclipse-hibernate 逆向工程插件自动生成的休眠 DAO 代码。

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

还有,这里是一个代码片段,我手动创建一个“Pokemons”对象并将其传递给“persist(..)”方法。

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

【问题讨论】:

    标签: java mysql database hibernate persist


    【解决方案1】:

    em.commit()

    出于性能原因,当您调用 em.persist() 时,Hibernate 会将对象持久化到底层模型,但在您提交事务之前不会真正执行数据库指令。

    【讨论】:

    • 问题解决了! (通过包装我的方法是以下语句) entityManager.getTransaction().begin(); entityManager.getTransaction().commit();
    • 如果你使用的是Spring,你可以在你的方法上注解@Transactional。
    猜你喜欢
    • 2020-01-22
    • 1970-01-01
    • 2019-01-29
    • 2011-03-02
    • 2012-05-30
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多