dushunwill
2010-04-21 13:52
浏览 508
已采纳

hibernate 出错 java.lang.ClassCastException

[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]

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • iteye_12115 2010-04-21 14:04
    已采纳

    你表是个复合主键,但是你的orm对象不是复合对象,需要更改映射的user对象才行。

    打赏 评论
  • zhangrunzgz 2010-04-21 14:35

    映射文件的格式改改不要[quote] [/quote]

    打赏 评论

相关推荐 更多相似问题