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

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对象才行。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python验证码滑块图像识别
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)