标签:
新建的对象立马被删除
@Test public void testDelete() { Teacher t = new Teacher(); t.setName("t1"); t.setTitle("middle"); t.setBirthDate(new Date()); Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); session.save(t); System.out.println(t.getId()); session.getTransaction().commit(); Session session2 = sessionFactory.getCurrentSession(); session2.beginTransaction(); session2.delete(t); session2.getTransaction().commit(); }
删除指定ID的对象:
@Test public void testDelete2() { Teacher t = new Teacher(); t.setId(2); Session session2 = sessionFactory.getCurrentSession(); session2.beginTransaction(); session2.delete(t); session2.getTransaction().commit(); }
load返回的代理对象, 真正用到对象时才发出sql语句,
get直接从数据库加载,发出sql语句, 不会延迟.
所以load必须在commit之前执行getName方法的操作.
但是get放在commit之前之后都可以.
@Test public void testLoad() { Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); Teacher t = (Teacher)session.load(Teacher.class, 1); //此数字是ID System.out.println(t.getName()); session.getTransaction().commit(); //System.out.println(t.getClass()); } @Test public void testGet() { Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); Teacher t = (Teacher)session.get(Teacher.class, 1); session.getTransaction().commit(); //System.out.println(t.getClass()); System.out.println(t.getName()); }
update:
第一种情况: 用给的detached对象来更新persistent对象.
@Test public void testUpdate1() { Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); Teacher t = (Teacher)session.get(Teacher.class, 1); //persistent session.getTransaction().commit(); t.setName("zhanglaoshi"); //commit后, detached Session session2 = sessionFactory.getCurrentSession(); session2.beginTransaction(); session2.update(t); //persistent session2.getTransaction().commit(); }
第二种情况:更新transient对象会报错
@Test public void testUpdate2() { Teacher t = new Teacher(); t.setName("zhanglaoshi"); Session session2 = sessionFactory.getCurrentSession(); session2.beginTransaction(); session2.update(t); session2.getTransaction().commit(); }
第三种情况, 手动设置ID后update, 数据库有对应记录
@Test public void testUpdate3() { Teacher t = new Teacher(); t.setId(3); t.setName("zhanglaoshi"); Session session2 = sessionFactory.getCurrentSession(); session2.beginTransaction(); session2.update(t); session2.getTransaction().commit(); }
第四种情况: 我们在update的时候, 没有设置别的字段, 但是控制台把所有字段都输出了,效率低下.
一个persistent状态的对象只要设定不同字段, 就会更新:
@Test public void testUpdate4() { Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); Teacher t = (Teacher)session.get(Teacher.class, 1); //Persistent状态 t.setName("zhangsan2"); //缓存和数据库内容不一致, commit时统一 session.getTransaction().commit(); }
最后都没有得到我们想要的效果: 只更新想改变的字段, 有3种方法:
1. xml和annotation设置
annotation方法:Teacher.java里不想更新的字段的get方法前,加入@Column(updatable=false)
xml方法: <property name="title" update=false> </property>
2. 使用xml的dynamic-update,
<hibernate-mapping> <class name="com.bjsxt.hibernate.Student" dynamic-update="true">
3. 推荐!!! HQL (EJBQL)
注意: update后面的Student是对象, 不是表名
@Test public void testUpdate7() { Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); Query q = session.createQuery("update Student s set s.name=‘z5‘ where s.id = 1"); q.executeUpdate(); session.getTransaction().commit(); }
hibernate--session的CRUD方法, delete, load,get,update
标签:
原文地址:http://www.cnblogs.com/wujixing/p/5416867.html