您的位置:首页 > 其它

Hibernate 实体基本增删查改操作

2014-07-10 21:03 351 查看
还是引用上面的TheClass 实体和Student实体 代码如下:<pre name="code" class="java">@Entity
@Table(name="tb_class")
public class TheClass {
private int classId;
private String className;
private Set<Student>students;
//getter setter 省略

<pre name="code" class="java">@Entity    // 表明该类为实体类
@Table(name="tb_student")  //映射数据库的表名为 tb_student
public class Student {
private int stuId;      // 学号
private String stuName; // 姓名
private boolean stuSex; //性别
private Date stuBirth; // 出生年月
private TheClass myClass;
private Set<StuCourse> stuCourses;   // 每个学生有多个选课记录
@Id


数据表的初始状态如下:



首先查找:
<pre name="code" class="java">Session session=HibernateSessionFactory.getSession();
session.beginTransaction();
TheClass c=(TheClass)session.get(TheClass.class, new Integer(1));  //根据Id得到班级实体
session.getTransaction().commit();
System.out.println("ClassName:"+c.getClassName());


生成的sql语句:
Hibernate:
select
theclass0_.classId as classId1_0_,
theclass0_.className as className1_0_
from
tb_class theclass0_
where
theclass0_.classId=?
增加学生实体:
session.beginTransaction();
Student stu=new Student("s1",true,new Date(),(TheClass)session.get(TheClass.class, new Integer(1)));
session.save(stu);  // 增加实体
session.getTransaction().commit();
查看sql语句:

Hibernate: 
    select
        theclass0_.classId as classId1_0_,
        theclass0_.className as className1_0_ 
    from
        tb_class theclass0_ 
    where
        theclass0_.classId=?
Hibernate: 
    insert 
    into
        tb_student
        (class_id, stu_birth, stu_name, stuSex) 
    values
        (?, ?, ?, ?)
修改学生实体:
<pre name="code" class="java">Student stu=new Student("s1",true,new Date(),(TheClass)session.get(TheClass.class, new Integer(1)));
session.beginTransaction();
stu.setStuName("ss1");
System.out.println("stuSex:"+stu.isStuSex());
session.update(stu);  // 修改实体,修改的实体必须要是 persistence 的
session.getTransaction().commit();
session.close();


运行出异常:
Exception in thread "main" org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:247)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1001)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:339)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at edu.hue.jk.test.TestMain.main(TestMain.java:30)
因为学生实体对象不是持久化对象,因此不能被修改,有关持久化参考:

http://blog.sina.com.cn/s/blog_674d42620100xky8.html
</pre><pre name="code" class="java">修改代码:<pre name="code" class="java">		session.beginTransaction();
Student stu=(Student)session.get(Student.class, new Integer(1));
stu.setStuName("ss1");
System.out.println("stuSex:"+stu.isStuSex());
session.update(stu);  // 修改实体,修改的实体必须要是 persistence 的
session.getTransaction().commit();
session.close();
生成的sql:

<pre name="code" class="java">Hibernate:
select
theclass0_.classId as classId1_0_,
theclass0_.className as className1_0_
from
tb_class theclass0_
where
theclass0_.classId=?
stuSex:true
Hibernate:
update
tb_student
set
class_id=?,
stu_birth=?,
stu_name=?,
stuSex=?
where
stu_id=?


最后删除:

<pre name="code" class="java">session.beginTransaction();
Student stu=(Student)session.get(Student.class, new Integer(1));
session.delete(stu);  // 删除实体,修改的实体必须要是 persistence 的
session.getTransaction().commit();
session.close();


sql语句:
Hibernate:
select
student0_.stu_id as stu1_0_1_,
student0_.class_id as class5_0_1_,
student0_.stu_birth as stu2_0_1_,
student0_.stu_name as stu3_0_1_,
student0_.stuSex as stuSex0_1_,
theclass1_.classId as classId1_0_,
theclass1_.className as className1_0_
from
tb_student student0_
left outer join
tb_class theclass1_
on student0_.class_id=theclass1_.classId
where
student0_.stu_id=?
Hibernate:
delete
from
tb_student
where
stu_id=?



</pre><pre name="code" class="java">
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: