数据库:MySql
数据表:
CREATE TABLE `mystudent` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(20) default NULL,
  `address` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk ROW_FORMAT=REDUNDANT;mystudent.hbm.xml:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="per.dao.MyStudent" table="mystudent">
<id name="id" column="id" type="java.lang.Long">
<generator class="identity" />
</id>
<property name="name" type="string" column="name"/>
<property name="address" type="string" column="address" />
</class>
</hibernate-mapping>bean:package per.dao;public class MyStudent{
private Long id;
public Long getId(){
return id;
}
public void setId(Long value){
this.id = id;
}

private String name = "";
public String getName(){
return name;
}
public void setName(String value){
name = value;
}

private String address = "";
public String getAddress(){
return address;
}
public void setAddress(String value){
address = value;
}
}
测试的java代码:
import org.hibernate.*;
import org.hibernate.cfg.*;
import per.dao.*;class HibernateUtil {    public static final SessionFactory sessionFactory;    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }    public static final ThreadLocal<Session> session = new ThreadLocal<Session>();    public static Session currentSession() throws HibernateException {
        Session s = session.get();
        // Open a new Session, if this thread has none yet
        if (s == null) {
            s = sessionFactory.openSession();
            // Store it in the ThreadLocal variable
            session.set(s);
        }
        return s;
    }

    public static void closeSession() throws HibernateException {
        Session s = (Session) session.get();
        if (s != null)
            s.close();
        session.set(null);
    }
}public class TestHibernate{
public static void main(String[] args){
Session session = HibernateUtil.currentSession();
Transaction trans = session.beginTransaction();

MyStudent stu = new MyStudent();
stu.setName("张三");
stu.setAddress("湖南");

session.save(stu);

trans.commit();
HibernateUtil.closeSession();
}
}
运行时出现如下错误,我看了错误是在 Transaction.commit()中,很奇怪啊:
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" org.hibernate.HibernateException: identifier of an in
stance of per.dao.MyStudent was altered from 8 to null
        at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(Defau
ltFlushEntityEventListener.java:51)
        at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(Def
aultFlushEntityEventListener.java:140)
        at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity
(DefaultFlushEntityEventListener.java:97)
        at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(A
bstractFlushingEventListener.java:195)
        at org.hibernate.event.def.AbstractFlushingEventListener.flushEverything
ToExecutions(AbstractFlushingEventListener.java:76)
        at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlus
hEventListener.java:26)
        at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:908)
        at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:344)
        at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java
:106)
        at TestHibernate.main(TestHibernate.java:52)

解决方案 »

  1.   

    id,还有实体类 用 Integer 类型映射试下看。
      

  2.   

    [align=left]    <property name="name" type="[color=#FF0000]string" column="name"/>
            <property name="address" type="string" column="address" />[/align][/color]
    配置文件的问题吧:
    identifier of an in
    stance of per.dao.MyStudent was altered 
    你再检查检查。
      

  3.   

    我已经找到原因了MyStudent.setId(Long value) 方法错误应该是 this.id = value;