当前位置: 首页 > 知识库问答 >
问题:

Hibernate实体管理器find()仍然可以检索删除hql的实体

柴嘉年
2023-03-14

以下是我的代码:

    @Test
    public void shouldRemovesAllFmReportFromDb()
    {
        FmReport fmReport1 = givenFmReportIsStoredInDb();
        FmReport fmReport2 = givenFmReportIsStoredInDb();
        FmReport fmReport3 = givenFmReportIsStoredInDb();

        Integer previousSize = getEntityManager().createQuery( "SELECT re FROM FmReport re" ).getResultList().size();
        System.out.println("*** before delete, the size is " + previousSize);

        FmReport fmReportById1 = getEntityManager().find(FmReport.class, fmReport1.getId());
        Assert.assertNotNull( fmReportById1 );


        getEntityManager().createQuery( "DELETE FROM FmReport" ).executeUpdate();

        Integer finalSize = getEntityManager().createQuery( "SELECT re FROM FmReport re" ).getResultList().size();
        System.out.println("*** after delete, the size is " + finalSize);

        Assert.assertNull( getEntityManager().find(FmReport.class, fmReport1.getId()));
    }

此测试失败,因为hql删除后,hibernate仍然可以找到删除的实体。

日志:

18:25:36.027 [main] DEBUG o.h.hql.internal.ast.ErrorCounter - throwQueryException() : no errors
18:25:36.029 [main] DEBUG o.h.hql.internal.ast.ErrorCounter - throwQueryException() : no errors
18:25:36.029 [main] DEBUG o.h.internal.SessionFactoryImpl - Checking 0 named SQL queries
18:25:36.042 [main] DEBUG o.h.s.internal.StatisticsInitiator - Statistics initialized [enabled=false]
18:25:36.105 [main] DEBUG o.h.e.i.EntityManagerFactoryRegistry - Initializing EntityManagerFactoryRegistry : org.hibernate.ejb.internal.EntityManagerFactoryRegistry@6ef93d8a
18:25:36.105 [main] DEBUG o.h.e.i.EntityManagerFactoryRegistry - Registering EntityManagerFactory: test-pu 18:25:36.216 [main] DEBUG o.h.e.t.spi.AbstractTransactionImpl - begin
18:25:36.216 [main] DEBUG o.h.e.j.i.LogicalConnectionImpl - Obtaining JDBC connection
18:25:36.216 [main] DEBUG o.h.e.j.i.LogicalConnectionImpl - Obtained JDBC connection
18:25:36.216 [main] DEBUG o.h.e.t.i.jdbc.JdbcTransaction - initial autocommit status: true
18:25:36.216 [main] DEBUG o.h.e.t.i.jdbc.JdbcTransaction - disabling autocommit
18:25:36.223 [main] DEBUG org.hibernate.SQL - select nextval ('fm_report_id_seq')
18:25:36.231 [main] DEBUG org.hibernate.id.SequenceGenerator - Sequence identifier generated: BasicHolder[java.lang.Long[1]]
18:25:36.234 [main] DEBUG o.h.e.i.AbstractSaveEventListener - Generated identifier: 1, using strategy: org.hibernate.id.SequenceHiLoGenerator
18:25:36.249 [main] DEBUG org.hibernate.SQL - select nextval ('fm_report_id_seq')
18:25:36.249 [main] DEBUG org.hibernate.id.SequenceGenerator - Sequence identifier generated: BasicHolder[java.lang.Long[2]]
18:25:36.249 [main] DEBUG o.h.e.i.AbstractSaveEventListener - Generated identifier: 2, using strategy: org.hibernate.id.SequenceHiLoGenerator
18:25:36.250 [main] DEBUG org.hibernate.SQL - select nextval ('fm_report_id_seq')
18:25:36.250 [main] DEBUG org.hibernate.id.SequenceGenerator - Sequence identifier generated: BasicHolder[java.lang.Long[3]]
18:25:36.250 [main] DEBUG o.h.e.i.AbstractSaveEventListener - Generated identifier: 3, using strategy: org.hibernate.id.SequenceHiLoGenerator
18:25:36.268 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Processing flush-time cascades
18:25:36.270 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Dirty checking collections
18:25:36.273 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Flushed: 3 insertions, 0 updates, 0 deletions to 3 objects
18:25:36.273 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
18:25:36.274 [main] DEBUG o.h.internal.util.EntityPrinter - Listing entities:
18:25:36.277 [main] DEBUG o.h.internal.util.EntityPrinter - FmReport{id=3, alarmsJson=jsonString, date=Thu Jan 01 08:00:00 CST 1970}
18:25:36.277 [main] DEBUG o.h.internal.util.EntityPrinter - FmReport{id=2, alarmsJson=jsonString, date=Thu Jan 01 08:00:00 CST 1970}
18:25:36.277 [main] DEBUG o.h.internal.util.EntityPrinter - FmReport{id=1, alarmsJson=jsonString, date=Thu Jan 01 08:00:00 CST 1970}
18:25:36.277 [main] DEBUG org.hibernate.engine.spi.ActionQueue - Changes must be flushed to space: fm_report
18:25:36.289 [main] DEBUG org.hibernate.SQL - insert into fm_report (alarms_json, date, id) values (?, ?, ?)
18:25:36.295 [main] DEBUG org.hibernate.SQL - insert into fm_report (alarms_json, date, id) values (?, ?, ?)
18:25:36.296 [main] DEBUG org.hibernate.SQL - insert into fm_report (alarms_json, date, id) values (?, ?, ?)
18:25:36.300 [main] DEBUG org.hibernate.SQL - select fmreport0_.id as id1_0_, fmreport0_.alarms_json as alarms_j2_0_, fmreport0_.date as date3_0_ from fm_report fmreport0_
18:25:36.302 [main] DEBUG org.hibernate.loader.Loader - Result set row: 0
18:25:36.306 [main] DEBUG org.hibernate.loader.Loader - Result row: EntityKey[FmReport#1]
18:25:36.307 [main] DEBUG org.hibernate.loader.Loader - Result set row: 1
18:25:36.308 [main] DEBUG org.hibernate.loader.Loader - Result row: EntityKey[FmReport#2]
18:25:36.309 [main] DEBUG org.hibernate.loader.Loader - Result set row: 2
18:25:36.309 [main] DEBUG org.hibernate.loader.Loader - Result row: EntityKey[FmReport#3]
*** before delete, the size is 3
18:25:36.322 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Processing flush-time cascades
18:25:36.323 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Dirty checking collections
18:25:36.323 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Flushed: 0 insertions, 0 updates, 0 deletions to 3 objects
18:25:36.324 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
18:25:36.324 [main] DEBUG o.h.internal.util.EntityPrinter - Listing entities:
18:25:36.324 [main] DEBUG o.h.internal.util.EntityPrinter - FmReport{id=3, alarmsJson=jsonString, date=Thu Jan 01 08:00:00 CST 1970}
18:25:36.324 [main] DEBUG o.h.internal.util.EntityPrinter - FmReport{id=2, alarmsJson=jsonString, date=Thu Jan 01 08:00:00 CST 1970}
18:25:36.324 [main] DEBUG o.h.internal.util.EntityPrinter - FmReport{id=1, alarmsJson=jsonString, date=Thu Jan 01 08:00:00 CST 1970}
18:25:36.330 [main] DEBUG org.hibernate.SQL - delete from fm_report
18:25:36.334 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Processing flush-time cascades
18:25:36.335 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Dirty checking collections
18:25:36.337 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Flushed: 0 insertions, 0 updates, 0 deletions to 3 objects
18:25:36.337 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
18:25:36.337 [main] DEBUG o.h.internal.util.EntityPrinter - Listing entities:
18:25:36.337 [main] DEBUG o.h.internal.util.EntityPrinter - FmReport{id=3, alarmsJson=jsonString, date=Thu Jan 01 08:00:00 CST 1970}
18:25:36.338 [main] DEBUG o.h.internal.util.EntityPrinter - FmReport{id=2, alarmsJson=jsonString, date=Thu Jan 01 08:00:00 CST 1970}
18:25:36.338 [main] DEBUG o.h.internal.util.EntityPrinter - FmReport{id=1, alarmsJson=jsonString, date=Thu Jan 01 08:00:00 CST 1970}
18:25:36.338 [main] DEBUG org.hibernate.SQL - select fmreport0_.id as id1_0_, fmreport0_.alarms_json as alarms_j2_0_, fmreport0_.date as date3_0_ from fm_report fmreport0_
*** after delete, the size is 0
18:25:36.338 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Processing flush-time cascades
18:25:36.339 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Dirty checking collections
18:25:36.339 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Flushed: 0 insertions, 0 updates, 0 deletions to 3 objects
18:25:36.339 [main] DEBUG o.h.e.i.AbstractFlushingEventListener - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
18:25:36.339 [main] DEBUG o.h.internal.util.EntityPrinter - Listing entities:
18:25:36.339 [main] DEBUG o.h.internal.util.EntityPrinter - FmReport{id=3, alarmsJson=jsonString, date=Thu Jan 01 08:00:00 CST 1970}
18:25:36.339 [main] DEBUG o.h.internal.util.EntityPrinter - FmReport{id=2, alarmsJson=jsonString, date=Thu Jan 01 08:00:00 CST 1970}
18:25:36.339 [main] DEBUG o.h.internal.util.EntityPrinter - FmReport{id=1, alarmsJson=jsonString, date=Thu Jan 01 08:00:00 CST 1970}
18:25:36.339 [main] DEBUG o.h.e.t.spi.AbstractTransactionImpl - rolling back
18:25:36.343 [main] DEBUG o.h.e.t.i.jdbc.JdbcTransaction - rolled JDBC Connection
18:25:36.343 [main] DEBUG o.h.e.t.i.jdbc.JdbcTransaction - re-enabling autocommit
18:25:36.346 [main] DEBUG o.h.e.j.internal.JdbcCoordinatorImpl - HHH000420: Closing un-released batch
18:25:36.346 [main] DEBUG o.h.e.j.i.LogicalConnectionImpl - Releasing JDBC connection
18:25:36.346 [main] DEBUG o.h.e.j.i.LogicalConnectionImpl - Released JDBC connection

java.lang.AssertionError: expected null, but was:<FmReport@88dfd83>

相同的代码适用于Eclipse Link,但在hibernate中,它没有。我在问背后的原因是什么,或者Hibernate所需的具体配置是什么?

共有1个答案

周祺
2023-03-14

更正是:entityManager.clear();应该在 sql 执行之前/之后运行。推荐的地方是之前。请检查何时使用EntityManager.clear()?

 类似资料:
  • 我有一个通用的JPA存储库实现,处理以下多种类型的实体: 在某个时候,我意识到一些实体还没有被删除。然后,我发现一些实体导致删除的取消,如下所述:https://stackoverflow.com/a/16901857/502059 由于该方法是通用的,因此它包含各种类型的实体。作为一种解决方法,我想摆脱导致取消删除的实体,并在 方法的开头添加了 虽然这有效,但我觉得这是一个肮脏的解决方法。 所以

  • 问题内容: 我有两个分别以双向一对多关系存在的实体类 A 和 B。 A.java: B.java 在一个简单的控制台应用程序中,我从数据库中获取了特定的 A 行,并尝试删除其详细信息 B 行(随机),但是 JPA / Hibernate* 不仅删除了该行-甚至没有向该行发出任何 DELETE 语句。数据库。删除 B 行的唯一方法是从 A.java 的集合( LinkedHashSet )中删除相应

  • 我正在使用Spring boot 1.2.3。JPA的发布版本超过Hibernate。我遇到以下异常 以下是我的程序结构 配置类 在上面的服务类代码中,有人能指导我为什么2个工作和1个抛出异常。 谢啦

  • 我试图使用spring boot将EntityManager注释注入到我的DAO中,但是得到一个< code > InvalidDataAccessApiUsageException 消息,说没有可用的事务EntityManager。我的印象是,只要Spring Boot从< code>application.yml中获得了我的数据源信息,并且我用< code>@PersitenceContext

  • 删除父实体时,我还想删除关联的子实体(从数据库中)。我试图在删除时使用级联,如下所示,但我一定做错了什么。 当对父实体对象调用删除时,我收到错误消息:“该实体仍在数据库的其他地方引用”。我可以确认该实体在数据库的其他地方引用的唯一地方是在下面的两个表中(如果我手动从数据库中删除子行,对父实体对象的删除调用工作正常)。在过去的9个小时里,我一直在阅读实体对象并尝试不同的东西。我做错了什么? 这是我的

  • 问题内容: 我在数据访问层中将JPA-2.0与Hibernate一起使用。 为了进行审核日志记录,我通过在persistence.xml中配置以下属性来使用Hibernate的EmptyInterceptor: 凡 AuditLogInterceptor 扩展Hibernate的’ org.hibernate.EmptyInterceptor ‘。 我在数据访问层中使用JPA实体管理器来执行数据库