2016-07-11 55 views
0

我試圖做一個StatelessSession的簡單保存。有人可以發現我做錯了,最終導致Hibernate方法嘗試對Session執行無效的StatelessSession轉換。我會很感激任何反饋。StatelessSession被轉換爲AbstractEntityPersister中的會話(休眠)

我的示例代碼: StatelessSession statelessSession = getSessionFactory().openStatelessSession(); Transaction tx = statelessSession.beginTransaction(); Object 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)調用發生的異常。跟蹤代碼登陸我在嘗試將要StatelessSession轉換爲會話此方法中的AbstractEntityPersister類:

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) 
+0

請堆棧跟蹤添加到上述問題的 –

+0

堆棧跟蹤加入。謝謝... – longtimer

回答

1

已經有一個問題,要求打開此!這應該在Hibernate中5.2.2

https://hibernate.atlassian.net/browse/HHH-11048

getEntityMetamodel().hasPreInsertGeneratedValues()返回true,你必須具有通過休眠插入之前生成的東西!

示例:時間戳或自動生成的ID等