Hibernate不会删除我的对象.为什么? [英] Hibernate is not deleting my objects. Why?

查看:69
本文介绍了Hibernate不会删除我的对象.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚设置了一个测试,以检查是否可以使用Hibernate将条目插入数据库.使我发疯的是,尽管它报告它们已消失,但Hibernate实际上并未删除这些条目!

I have just set up a test that checks that I am able to insert entries into my database using Hibernate. The thing that drives me crazy is that Hibernate does not actually delete the entries, although it reports that they are gone!

下面的测试成功运行,但是之后我检查数据库时,插入的条目仍然存在!我什至尝试使用 assert 进行检查(是的,我将-ea作为vm参数).有谁知道为什么不删除条目的线索?

The test below runs successfully, but when I check my DB afterwards the entries that were inserted are still there! I even try to check it using assert (yes I have -ea as vm parameter). Does anyone have a clue why the entries are not deleted?

public class HibernateExportStatisticDaoIntegrationTest {
    HibernateExportStatisticDao dao;
    Transaction transaction;

    @Before
    public void setUp(){
        assert numberOfStatisticRowsInDB() == 0;
        dao = new HibernateExportStatisticDao(HibernateUtil.getSessionFactory());
    }

    @After
    public void deleteAllEntries(){
        assert numberOfStatisticRowsInDB() != 0;
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        for(PersistableStatisticItem item:allStatisticItemsInDB()) {
            session.delete(item);
        }
        session.flush();
        assert numberOfStatisticRowsInDB() == 0;
    }

    @Test public void exportAllSavesEntriesToDatabase(){
        int expectedNumberOfStatistics = 20;
        dao.exportAll(StatisticItemFactory.createTestStatistics(expectedNumberOfStatistics));

        assertEquals(expectedNumberOfStatistics, numberOfStatisticRowsInDB());
    }

    private int numberOfStatisticRowsInDB() {
        return allStatisticItemsInDB().size();
    }

    @SuppressWarnings("unchecked")
    private List<PersistableStatisticItem> allStatisticItemsInDB(){
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        transaction = session.beginTransaction();
        Query q = session.createQuery("FROM PersistableStatisticItem item");
        return q.list();
    }
}

控制台充满

Hibernate: delete from UPTIME_STATISTICS where logDate=? and serviceId=?

但是我检查时什么也没有被删除.

but nothing has been deleted when I check it.

推荐答案

我想这与事务使用不一致有关(请注意,allStatisticItemsInDB()中的beginTransaction()被多次调用而没有相应的提交).

I guess it's related to inconsistent use of transactions (note that beginTransaction() in allStatisticItemsInDB() is called several times without corresponding commits).

尝试以适当的方式管理交易,例如:

Try to manage transactions in proper way, for example, like this:

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
for(PersistableStatisticItem item:
    session.createQuery("FROM PersistableStatisticItem item").list()) {
    session.delete(item);
}
session.flush();
assert session.createQuery("FROM PersistableStatisticItem item").list().size() == 0;
tx.commit();

另请参见:

这篇关于Hibernate不会删除我的对象.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆