我正在尝试使用带有以下代码的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 Framework和Hibernate 5.3.7.Final。我也遵循了Hibernate 5.3.7.Final上的教程,但无济于事。
有人在此StackOverflow question 上提到这是一个应该在5.2.2上修复的错误,但我使用的是5.3.7.Final,仍然出现该错误。
有人知道如何摆脱这个错误吗?还是在Hibernate中获取StatelessSession实例的正确方法?
答案 0 :(得分:0)
根据该错误消息,该会话已经是一个StatelessSession
,因此您应该能够直接将其解开:
final StatelessSession session = entityManager.unwrap(StatelessSession.class);