您的位置:首页 > 其它

hibernate核心类和接口

2016-04-18 15:58 183 查看
Part 1 Configuration类



Part 2 SessionFactory接口



通过SessionFactory获取Session有两个方法:openSession()和getCurrentSession

1、openSession()是获取一个新的session

2、getCurrentSession()获取和当前线程绑定的session,换言之,在同一个线程中,我们获取的session是同一session,利于事务的控制。

3、如果使用getCurrentSession需要在hibernate.cfg.xml中配置。

<property name="current_session_context_class">thread</property>

Part 3 Session接口



Part 4 Transaction接口



Part 5 Query接口



在实际应用中,Query接口十分常用,有更为强大的查询能力,而get、load只能根据主键查询,所以Query十分必要,Query可防止sql注入

Part 6 Criteria接口

Criteria接口也可用于面向对象方式的查询,实际使用的并不多。

//id指的是映射对象的属性名称而不是表的字段名
Query query=session.createQuery("from Employee where id=100");
List<Employee> list=query.list();
for(Employee e:list){
System.out.println(e.getName()+""+e.getHiredate());
}
Criteria cri=session.createCriteria(Employee.class).add(Restrictions.like("name","Fritz%")).setMaxResult(2);
List<Employee> list=cri.list();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: