hibernate 插入数据失败

xiaozhimin1978 2008-05-18 01:35:32
数据库: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)






...全文
121 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaozhimin1978 2008-05-19
  • 打赏
  • 举报
回复
我已经找到原因了

MyStudent.setId(Long value) 方法错误

应该是 this.id = value;

  • 打赏
  • 举报
回复
<property name="name" type="[color=#FF0000]string
" column="name"/>
<property name="address" type="string" column="address" />[/color]
配置文件的问题吧:
identifier of an in
stance of per.dao.MyStudent was altered
你再检查检查。
jdlsfl 2008-05-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 caoyinghui1986 的回复:]
id,还有实体类 用 Integer 类型映射试下看。
[/Quote]
Shine_Panda 2008-05-18
  • 打赏
  • 举报
回复
id,还有实体类 用 Integer 类型映射试下看。

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧