问题描述
我正在尝试在StatelessSession上进行简单保存。有人可以发现我做错的事情最终导致尝试执行对StatelessSession到Session的无效转换的Hibernate方法。
I'm trying to do a simple save on a StatelessSession. Can someone spot what I'm doing wrong to end up in the Hibernate method that tries to do an invalid cast of StatelessSession to Session. I would appreciate any feedback.
我的示例代码:
StatelessSession statelessSession = getSessionFactory()。openStatelessSession();
交易tx = statelessSession.beginTransaction();
对象ret = null;
try {
ret = statelessSession.insert(obj);
tx.commit();
statelessSession.close();
} catch(Throwable t){
System.out.println( exception: + t.getMessage());
tx.rollback();
}
return(T)ret;
该异常发生在.insert(obj)调用上。跟踪代码使我进入AbstractEntityPersister类,该类尝试使用此方法将StatelessSession转换为Session:
The exception occurs on the .insert(obj) call. Tracing the code lands me in the AbstractEntityPersister class which attempts to cast that StatelessSession to Session in this method:
private void preInsertInMemoryValueGeneration(Object[] fields, Object object, SessionImplementor session) {
if ( getEntityMetamodel().hasPreInsertGeneratedValues() ) {
final InMemoryValueGenerationStrategy[] strategies = getEntityMetamodel().getInMemoryValueGenerationStrategies();
for ( int i = 0; i < strategies.length; i++ ) {
if ( strategies[i] != null && strategies[i].getGenerationTiming().includesInsert() ) {
fields[i] = strategies[i].getValueGenerator().generateValue( (Session) session, object ); // <-- bad cast here
setPropertyValue( object, i, fields[i] );
}
}
}
}
堆栈跟踪:
java.lang.ClassCastException: org.hibernate.internal.StatelessSessionImpl cannot be cast to org.hibernate.Session
at org.hibernate.persister.entity.AbstractEntityPersister.preInsertInMemoryValueGeneration(AbstractEntityPersister.java:3591)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3568)
at org.hibernate.internal.StatelessSessionImpl.insert(StatelessSessionImpl.java:144)
at org.hibernate.internal.StatelessSessionImpl.insert(StatelessSessionImpl.java:123)
at com.mycompany.somepackage.dao.impl.CommonDAOImpl.persist2(CommonDAOImpl.java:242)
at com.mycompany.somepackage.publisher.ScJournalPublisher.publish(ScJournalPublisher.java:37)
at com.mycompany.somepackage.annotation.callback.CallbackInvoker.invokePublisherCallback(CallbackInvoker.java:92)
at com.mycompany.somepackage.event.listener.DaPostInsertUpdateEventListener.onPostInsert(DaPostInsertUpdateEventListener.java:51)
at org.hibernate.action.internal.EntityInsertAction.postInsert(EntityInsertAction.java:177)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:145)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1258)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:584)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:521)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
推荐答案
已经为此发出了问题请求!这应该在Hibernate 5.2.2中可用
There is already an issue request opened for this! This should be available in Hibernate 5.2.2
getEntityMetamodel()。hasPreInsertGeneratedValues()
返回true,因为在插入之前您必须拥有hibernate生成的东西!
getEntityMetamodel().hasPreInsertGeneratedValues()
returns true as you must be having something which is generated by hibernate before insert!
示例:时间戳或自动生成的ID等。
Example : Timestamp or Auto generated ids etc.
这篇关于StatelessSession被强制转换为AbstractEntityPersister(Hibernate)中的Session的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!