2014-07-15 124 views
4

我面臨着Hibernate的問題。Hibernate問題java.lang.Boolean不能轉換爲java.lang.String使用JDK 1.7

使用Hibernate 3.2.6和JDK 1.7.0_21

這是問題,因爲來JDK兼容性與Hibernate的版本?

此問題是隨機的。我仍然無法找到重現的步驟。

2014-07-14 06:09:10,661 [DEBUG] EventExpenseAreaService.getEventSummary:654 - Revenue Value (Hari) --> 1166.15 
2014-07-14 06:09:18,665 [ERROR] EventSetupService.getEventById:1451 - java.lang.Boolean cannot be cast to java.lang.String 
java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String 
    at org.hibernate.type.StringType.toString(StringType.java:44) 
    at org.hibernate.type.NullableType.nullSafeToString(NullableType.java:93) 
    at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:140) 
    at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:107) 
    at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2002) 
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2376) 
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2312) 
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2612) 
    at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:96) 
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279) 
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263) 
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168) 
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) 
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) 
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) 
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338) 
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106) 
    at com.sibisoft.northstar.events.service.EventSetupService.getEventById(EventSetupService.java:1441) 
    at com.sibisoft.northstar.events.struts.EventAction.load(EventAction.java:1037) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 

代碼getEventById

public EventDTO getEventById(Integer eventId,boolean logActivity, Session session)throws Exception { 

    EventDTO event = null; 
    Transaction transaction = null; 
    try { 

     if (session == null) { 
      session = HibernateSessionFactory.getSession(); 
      if(logActivity){ 
       transaction = session.beginTransaction(); 
      } 
     } 

     event = (EventDTO) super.getByPrimaryKey(EventDTO.class, eventId,session); 


     if(transaction!=null){ 
      transaction.commit(); 
     } 

    } catch (HibernateException e) { 
     LOGGER.error(e.getMessage(), e); 
     if(transaction!=null){ 
      transaction.rollback(); 
     } 
     throw e; 
    }catch (Exception e) { 
     LOGGER.error(e.getMessage(), e); 
     if(transaction!=null){ 
      transaction.rollback(); 
     } 
     throw e; 
    } 

    return event; 

} 

方法:getByPrimaryKey

protected BaseEventDTO getByPrimaryKey(Class clazz, Integer pk,Session session) throws Exception{ 

    BaseEventDTO dto = null; 
    Transaction tx = null; 

    try { 

     if (session == null) { 
      session = HibernateSessionFactory.getSession(); 
     } 
     dto = (BaseEventDTO) session.get(clazz, pk); 
     return dto; 

    } 
    catch(Exception e){ 
     LOGGER.error(e); 
     if (tx !=null) { 
      tx.rollback(); 
     } 
     throw e; 
    } 
} 
+0

顯示您的代碼:com.sibisoft.northstar.events.service.EventSetupService.getEventById() –

+0

見代碼,我有我的問題 –

回答

6
JDK

7已經改變Class.getDeclaredMethods()這樣的順序不被保證。 [click here]

您可能在對象映射中有一個屬性,該屬性的getter方法String getProperty()以及Boolean isPropery()會間歇性地導致問題。

休眠3 BasicPropertyAccessor.getterMethod(...)一些時間發現getProperty()和一些時間isProperty()由於getDeclaredMethods()在JDK 7未指定的排序。這會混淆hibernate,它會爲String類型屬性調用Boolean類型方法。

您需要重命名一個方法才能獲得預期結果。 Hibernate的論壇

類似的問題:https://forum.hibernate.org/viewtopic.php?p=2474641

+0

感謝伊利亞斯添加代碼。它爲我工作:) –

相關問題