【问题标题】:How do I get a StatelessSession when using Hibernate使用 Hibernate 时如何获得 StatelessSession
【发布时间】:2018-11-13 09:08:19
【问题描述】:

我正在尝试使用带有以下代码的 SessionFactory 获取 StatelessSession:

final Session session = entityManager.unwrap(Session.class);
final SessionFactory sessionFactory = session.getSessionFactory();
final StatelessSession statelessSession = sessionFactory.openStatelessSession();

但是当我测试代码时它失败并出现以下异常:

java.util.concurrent.ExecutionException: com.lightbend.lagom.javadsl.api.transport.TransportException: java.lang.ClassCastException: org.hibernate.internal.StatelessSessionImpl cannot be cast to org.hibernate.Session
at org.hibernate.persister.entity.AbstractEntityPersister.preInsertInMemoryValueGeneration(AbstractEntityPersister.java:3696)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3673)
at org.hibernate.internal.StatelessSessionImpl.insert(StatelessSessionImpl.java:111)
at org.hibernate.internal.StatelessSessionImpl.insert(StatelessSessionImpl.java:87)
at z.c.e.z.b.r.JPABatchRepository.lambda$null$0(JPABatchRepository.java:49)
at play.db.jpa.DefaultJPAApi.withTransaction(DefaultJPAApi.java:142)
at play.db.jpa.DefaultJPAApi.withTransaction(DefaultJPAApi.java:100)
at z.c.e.z.b.r.JPABatchRepository.wrap(JPABatchRepository.java:42)
at z.c.e.z.b.r.JPABatchRepository.lambda$createBatch$1(JPABatchRepository.java:47)
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

我已遵循本教程Working with stateless session in hibernate and jpa,但它似乎对我不起作用。我正在使用 Lagom 框架和 Hibernate 5.3.7.Final。我也按照Hibernate 5.3.7.Final 上的教程进行操作,但无济于事。

有人在StackOverflow question 上提到这是一个应该在 5.2.2 上修复的错误,但我正在使用 5.3.7.Final 并且仍然收到错误。

有谁知道如何摆脱这个错误?还是在 Hibernate 中获取 StatelessSession 实例的正确方法?

【问题讨论】:

  • 您好,您找到解决方案了吗?如果是,您能解释一下吗

标签: hibernate lagom


【解决方案1】:

根据那个错误信息,会话已经是StatelessSession,所以你应该可以直接将它解包为一个:

final StatelessSession session = entityManager.unwrap(StatelessSession.class);

【讨论】:

  • 如果我这样做,我会得到:PersistenceException: Hibernate cannot unwrap interface org.hibernate.StatelessSession
猜你喜欢
  • 2012-11-23
  • 2013-03-05
  • 2015-04-30
  • 2012-11-26
  • 2018-10-07
  • 2011-10-25
  • 2016-11-13
  • 1970-01-01
  • 2013-10-05
相关资源
最近更新 更多