首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

(六)hibernate CURD

2012-10-11 
(6)hibernate CURD1. 获取session,SessionFactory sf new AnnotationConfiguration().configure().build

(6)hibernate CURD

1. 获取session,SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();

A.Session session = sf.openSession();

B.Session session = sf.getCurrentSession();

?

说明:第一种每次都打开一个新连接,要显示关闭.

???????? 第二种,先从上下文查看有没有session,有就用它没有创建一个新的,一旦commit().就会自动关闭连接,在配置文件中hibernate.cfg.xml 要加入?? <property name="current_session_context_class">thread</property>也可能用jta.

?

2. save

Session session = sf.openSession();
??session.beginTransaction();
??session.save(s);
??session.getTransaction().commit();
? session.close();

?

下面可能看出session的关系:

Teacher t = new Teacher();
??t.setName("王老师");
??
??Session session = sf.getCurrentSession();
??session.beginTransaction();
??session.save(t);
??Session session2 = sf.getCurrentSession();
??
??System.out.println(session == session2);
??
??session.getTransaction().commit(); //提交之后此session关闭
??
??Session session3 = sf.getCurrentSession();
??System.out.println(session == session3);

?

3. delete

?

Teacher t = new Teacher();
??t.setName("Miss Wang");
??
??Session session = sf.getCurrentSession();
??session.beginTransaction();
??session.save(t);
??System.out.println(t.getId());
??session.getTransaction().commit();
??
??Session session2 = sf.getCurrentSession();
??session2.beginTransaction();
??session2.delete(t);
??session2.getTransaction().commit();

?

其实只要Id与DB中的Id对应就可以删除,不用管对象处在哪种状态.

?

4.Load

?

Session session = sf.getCurrentSession();
??session.beginTransaction();
??Teacher? t = (Teacher)session.load(Teacher.class, 2);
??System.out.println(t.getName());
??session.getTransaction().commit();
??System.out.println(t.getClass());

?

查出来的对象是一个代理类,只有去查类中的属性才会真正去查数据库.

?

5. Get

?

Session session = sf.getCurrentSession();
??session.beginTransaction();
??Teacher t = (Teacher)session.get(Teacher.class, 2);
??session.getTransaction().commit();
??System.out.println(t.getName());

?

不会产生廷迟加载,立即查询数据库.

?

6. Update

?

下面列举4种:

A.? update

Session session = sf.getCurrentSession();
??session.beginTransaction();
??Teacher t = (Teacher)session.get(Teacher.class, 2);
??session.getTransaction().commit();
??
??t.setName("李老师");
??
??Session session2 = sf.getCurrentSession();
??session2.beginTransaction();
??session2.update(t);
??session2.getTransaction().commit();

?

B.

Session session = sf.getCurrentSession();
??session.beginTransaction();
??Teacher t = (Teacher)session.get(Teacher.class, 2);
??t.setName("白老师");
??session.getTransaction().commit();

?

C.? HQL

Session session = sf.getCurrentSession();
??session.beginTransaction();
??Query query = session.createQuery("update Teacher set name='小明' where id=2");
??query.executeUpdate();
??session.getTransaction().commit();

?

D.? 合并

Session session = sf.getCurrentSession();
??session.beginTransaction();
??
??Teacher t = (Teacher)session.get(Teacher.class, 2);
??session.getTransaction().commit();
??
??t.setSex("女");
??Session session2 = sf.getCurrentSession();
??session2.beginTransaction();
??session2.merge(t);
??session2.getTransaction().commit();

?

说明:如果没有做什么设置,除了C以外,其它的更新操作如果你只是对其中一个字段更新hibernate还是会默认更新所有字段,解决方案有使用B或D,在xx.hbm.xml文件中,加入动态更新设置,如下

<class name="Teacher" dynamic-update="true">
??<id name="id">
???<generator class="native"/>
??</id>
??<property name="name"/>
??<property name="sex"/>
?</class>

?

这样只有要更新的字段才会更新,对于D方案还要提一下,当session提交之后对象处于detached,使用merget()方法,它会先去数据库先查一下放在缓存中与对象比较,然后才能判断哪个字段需要更新.

?

?

7. SaveOrUpdate

?

Teacher t = new Teacher();
??t.setName("XY");
??
??Session session = sf.getCurrentSession();
??session.beginTransaction();
??session.saveOrUpdate(t);
??session.getTransaction().commit();
??
??t.setName("JJ");
??Session session2 = sf.getCurrentSession();
??session2.beginTransaction();
??session2.saveOrUpdate(t);
??session2.getTransaction().commit();

?

8. Clear 清除缓存

?

Session session = sf.getCurrentSession();
??session.beginTransaction();
??Teacher t1 = (Teacher)session.load(Teacher.class, 3);
??System.out.println(t1.getName());
??System.out.println(t1.getSex());
?
??session.clear();
?
??Teacher t2 = (Teacher)session.load(Teacher.class, 3);
??System.out.println(t2.getName());
??
??session.getTransaction().commit();

?

?

?9.Flush

?

Session session = sf.getCurrentSession();
??session.beginTransaction();
??Teacher t = (Teacher)session.load(Teacher.class, 2);
??t.setName("ss");
??session.flush();
??t.setName("mm");
??session.getTransaction().commit();

?

10. SchemaExport

?

new SchemaExport(new AnnotationConfiguration().configure()).create(true, false);

?

说明:create(true,false)第一个boolean代表是否输出ddl,第二个,是否同步数据库.

?

热点排行