Hibernate问题java.lang.Boolean不能转换为java.lang.String使用JDK 1.7 [英] Hibernate issue java.lang.Boolean cannot be cast to java.lang.String Using 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 - 收入价值(Hari) - > 1166.15
2014年7月14日06:09:18665 [错误] EventSetupService.getEventById:1451 - java.lang.Boolean中不能转换为java.lang.String
java.lang.ClassCastException:JAVA。 lang.Boolean不能被转换为java.lang.String
在org.hibernate.type.StringType.toString(StringType.java:44)
在org.hibernate.type.NullableType.nullSafeToString(NullableType。的java:在org.hibernate.type.NullableType.nullSafeSet(NullableType.java:140 93)
)
。在org.hibernate.type.NullableType.nullSafeSet(NullableType.java:107)
在org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2002)
在org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2376)
在org.hibernate作为.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2312)
在org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2612)
在org.h ibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:96)
在org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
在org.hibernate.engine.ActionQueue.executeActions( ActionQueue.java:263)
在org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168)
在org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)在org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
在org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
。在组织.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
在org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
在com.sibisoft.northstar.events.service .EventSetupService.getEventById(EventSetupService.java:1441)
在com.sibisoft.northstar.events.struts.EventAction.load(EventAction.java:1037)
在sun.reflect.N ativeMethodAccessorImpl.invoke0(本机方法)
。在sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)
。在sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)
Code getEventById
public EventDTO getEventById(Integer eventId ,布尔logActivity,会话会话)抛出异常{
EventDTO event = null;
交易交易=空;
尝试{
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;
}
返回事件;
$ b 方法:getByPrimaryKey
'pre> 保护BaseEventDTO getByPrimaryKey(类clazz所整数PK,会话会话)抛出异常{
BaseEventDTO DTO = NULL;
Transaction tx = null;
try {
if(session == null){
session = HibernateSessionFactory.getSession();
}
dto =(BaseEventDTO)session.get(clazz,pk);
return dto;
catch(例外e){
LOGGER.error(e);
if(tx!= null){
tx.rollback();
}
throw e;
}
}
7已经改变了 Hibernate 3 您需要重命名一个方法才能获得预期的结果。 关于Hibernate论坛的类似问题: https://forum.hibernate.org/viewtopic.php?p = 2474641 I am facing issue with Hibernate. Using Hibernate 3.2.6 and JDK 1.7.0_21 Is this issue coming due to JDK compatibility with Hibernate version? This issue is random. I still unable to find steps to reproduce. Code getEventById Method : getByPrimaryKey
JDK 7 have changed You might have a property in Object mapping that have getter method Hibernate 3 You need to rename one method to get expected results. Similar question on Hibernate Forum: https://forum.hibernate.org/viewtopic.php?p=2474641 这篇关于Hibernate问题java.lang.Boolean不能转换为java.lang.String使用JDK 1.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! Class.getDeclaredMethods()
,所以顺序不能保证。 String getProperty() / code>以及布尔isPropery()
间歇性地导致问题。
BasicPropertyAccessor.getterMethod(...)
有些时候发现 getProperty()由于JDK 7中的未指定顺序由
getDeclaredMethods()
和某些时间 isProperty()
。它混淆了hibernate,它为String类型属性调用布尔类型方法。
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)
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;
}
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;
}
}
Class.getDeclaredMethods()
so the order is not guaranteed. [click here]String getProperty()
as well as Boolean isPropery()
that is causing problem intermittently. BasicPropertyAccessor.getterMethod(...)
some time finds getProperty()
and some time isProperty()
due to unspecified ordering in JDK 7 by getDeclaredMethods()
. which confuses hibernate and it invokes Boolean Type method for String type property.