hibernate出现异常
执行hibernate测试类时出现异常,测试代码如下:
import com.pojo.Person;
import com.sessionFactory.HibernateSessionFactory;
import com.sun.jmx.trace.TraceDestination;
;
public class Test {
public static void main(String args[])
{
Session session = HibernateSessionFactory.getSession();
Transaction transaction = session.beginTransaction();
Person p = new Person();
p.setId("3");
p.setName("zhangsan ");
p.setAge("23");
session.save(p);
transaction.commit();
HibernateSessionFactory.closeSession();
}
}
异常信息:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.ClassCastException: java.lang.String
at org.hibernate.type.ComponentType.toLoggableString(ComponentType.java:329)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:106)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:530)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:518)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:514)
at test.Test.main(Test.java:22)
请指点一下,谢谢
[解决办法]
结帖率:0.00%
[解决办法]
实在是坑爹,最近啊
[解决办法]
第22行
[解决办法]
第22行String类型转换异常
[解决办法]
p.setAge("23");
年龄是不是数值型??
[解决办法]
你的配置的 EntityMode 是 POJO, DOM4J 还是 MAP?
从 Hibernate 代码看,它说你的 EntityMode 无法猜测出来。
public String toLoggableString(Object value, SessionFactoryImplementor factory) throws HibernateException { if ( value == null ) { return "null"; } Map result = new HashMap(); EntityMode entityMode = tuplizerMapping.guessEntityMode( value ); if ( entityMode == null ) { throw new ClassCastException( value.getClass().getName() ); } Object[] values = getPropertyValues( value, entityMode ); for ( int i = 0; i < propertyTypes.length; i++ ) { result.put( propertyNames[i], propertyTypes[i].toLoggableString( values[i], factory ) ); } return StringHelper.unqualify( getName() ) + result.toString(); }