这里我们建立一个HibernateTest测试类。其中放置了Session和Transaction成员变量,这个在开发中不能放置为成员变量,会有并发问题的,这里我们只是测试可以放置一下。 我们通过单元测试的 @Before public void init()来初始化我们成员变量,然后通过@After public void destroy() 来关闭。
Hibernate: select news0_.hb_id as hb1_0_0_, news0_.hb_title as hb2_0_0_, news0_.hb_author as hb3_0_0_, news0_.hb_date as hb4_0_0_ from atguigu.hb_news news0_ where news0_.hb_id=? News{id=1, title='java', author='sun', date=2017-10-18}
Hibernate: select news0_.hb_id as hb1_0_0_, news0_.hb_title as hb2_0_0_, news0_.hb_author as hb3_0_0_, news0_.hb_date as hb4_0_0_ from atguigu.hb_news news0_ where news0_.hb_id=? News{id=1, title='java', author='mamh', date=2017-10-18}
=destroy= Hibernate: update atguigu.hb_news set hb_title=?, hb_author=?, hb_date=? where hb_id=?
Hibernate: select news0_.hb_id as hb1_0_0_, news0_.hb_title as hb2_0_0_, news0_.hb_author as hb3_0_0_, news0_.hb_date as hb4_0_0_ from atguigu.hb_news news0_ where news0_.hb_id=? Hibernate: select news0_.hb_id as hb1_0_0_, news0_.hb_title as hb2_0_0_, news0_.hb_author as hb3_0_0_, news0_.hb_date as hb4_0_0_ from atguigu.hb_news news0_ where news0_.hb_id=?
@Test public void testPersist() { //和save()方法很类似,区别: //在调用persist方法之前,调用了setId(),对象已经有了ID了,则不会执行insert操作,会抛出异常 News news = new News(); news.setAuthor("mm"); news.setTitle("ssssssssss"); news.setDate(new Date(new java.util.Date().getTime())); news.setId(234234); session.persist(news); }
@Test public void testSave() { //把临时对象变为持久化对象 //为对象分配ID,在save()方法之前设置ID是无效的,save()之后也是不能改这个ID的 //持久化的对象的ID是不能进行修改的 //在flush缓存时候会发送一条insert语句 //把临时对象保存到数据库中 News news = new News(); news.setAuthor("mm"); news.setTitle("ssssssssss"); news.setDate(new Date(new java.util.Date().getTime()));
session.save(news); }
session中的get()和load()方法 load和get的区别: get会立即加载对象,load方法若不用该对象则不会立即查询,而返回一个代理对象。 get立即检索,load是延迟检索。 若数据表中没有对应的记录,session也没有关闭,同时要使用该对象时候:get返回null,load抛出异常。 load方法可能会抛出懒加载异常: org.hibernate.LazyInitializationException: could not initialize proxy - no Session 在需要初始化代理对象之前关闭了session就会抛出这个异常。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
@Test public void testLoad(){ //load和get的区别: //get会立即加载对象,load方法若不用该对象则不会立即查询,而返回一个代理对象。 //get立即检索,load是延迟检索。 //若数据表中没有对应的记录,session也没有关闭,同时要使用该对象时候:get返回null,load抛出异常。 //load方法可能会抛出懒加载异常: // org.hibernate.LazyInitializationException: could not initialize proxy - no Session // 在需要初始化代理对象之前关闭了session就会抛出这个异常。 News news1 = (News) session.load(News.class, 10); //session.close(); System.out.println(news1); }
Hibernate: select news0_.hb_id as hb1_0_0_, news0_.hb_title as hb2_0_0_, news0_.hb_author as hb3_0_0_, news0_.hb_date as hb4_0_0_ from atguigu.hb_news news0_ where news0_.hb_id=? News{id=1, title='java', author='oracle', date=2017-10-18}
Hibernate: update atguigu.hb_news set hb_title=?, hb_author=?, hb_date=? where hb_id=?
@Test public void testSaveOrUpdate() { News news = new News("ff", "fff", new Date(new java.util.Date().getTime())); news.setId(1); session.saveOrUpdate(news); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Hibernate: select news_.hb_id, news_.hb_title as hb2_0_, news_.hb_author as hb3_0_, news_.hb_date as hb4_0_ from atguigu.hb_news news_ where news_.hb_id=? Hibernate: update atguigu.hb_news set hb_title=?, hb_author=?, hb_date=? where hb_id=?
如果有id,但是没有对应的记录,这个时候会抛出异常
1 2 3 4 5 6 7
@Test public void testSaveOrUpdate() { News news = new News("ff", "fff", new Date(new java.util.Date().getTime())); news.setId(23121); session.saveOrUpdate(news); }