[size=medium]刚刚学hibernate ,运行时出现异常
Exception in thread "main" java.lang.ClassCastException: java.lang.String
at org.hibernate.type.ComponentType.toLoggableString(ComponentType.java:377)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:112)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(
下面是代码:
[code="java"]
public class TestHibernate {
Configuration cfg;
SessionFactory sf;
Session session;
public static void main(String[] args){
TestHibernate test = new TestHibernate();
test.work();
}
public void work(){
cfg = new Configuration().configure();
sf = cfg.buildSessionFactory();
session = sf.openSession();
User user = new User();
user.setUserID("22");
user.setPassword("d");
user.setUsername("c");
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
}
}
[/code]
pojo的代码:
[code="java"]
public class User implements Serializable {
private String userID;
private String username;
private String password;
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
[/code]
[code="java"]
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
[/code][/size]