深入理解hibernate中的API
来源:互联网 发布:java输入流和输出流 编辑:程序博客网 时间:2024/05/17 01:20
这里我对hibernate中的一些API进行了一些详细的理解与分析:
package com.hibernate.demo1;import java.util.List;import org.hibernate.Criteria;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.Transaction;import org.hibernate.criterion.Restrictions;import org.junit.Test;import com.hibernate.utils.HibernateUtils;/*** * hibernate中的API详解 * @author Administrator * */public class HibernateTest2 { @Test /**** * Session对象------>save(arg); */ public void demo1(){ //1通过工具类获取Session Session session = HibernateUtils.openSession(); //2开启事务 Transaction tx = session.beginTransaction(); //5操作 //向数据库插入一条记录 Customer customer = new Customer(); customer.setName("小王2"); customer.setAge(26); session.save(customer); //6事务提交 tx.commit(); //7释放资源 session.close(); } @Test //保存或更新 /**** * Session对象------>saveOrUpdate(arg); */ public void demo2(){ //1通过工具类获取Session Session session = HibernateUtils.openSession(); /*2开启事务 【 如果没有这句话和tx.commit(),就没有开启事务, 因此以下每执行一个对数据库的操作都是一个单独的事务(所有操作都不在一个事务里), 并且每次执行完一个事务都会自动回滚】*/ Transaction tx = session.beginTransaction(); //5操作 //向数据库插入一条记录 Customer customer = new Customer(); customer.setName("小王4"); customer.setAge(26); /*使用【saveOrUpdate(customer)】对数据库进行【插入或更新】, 如果数据库已经存在与之对应的对象会执行【update】操作, 如果不存在则执行【save】操作*/ session.saveOrUpdate(customer); //检测提交前是否提交事务操作 false System.out.println(tx.wasCommitted()); //6事务提交 tx.commit(); //检测提交后是否提交事务操作 true System.out.println(tx.wasCommitted()); //7释放资源 session.close(); } @Test /**** * Query对象------>HQL操作 */ public void demo3(){ //1通过工具类获取Session Session session = HibernateUtils.openSession(); //2开启事务 Transaction tx = session.beginTransaction(); //5操作 /** HQL查询所有操作,无条件查询【不带参数】【Customer代表Customer对象,要与对象名称一致】 **/ //Query query = session.createQuery("from Customer"); /** HQL查询操作,有条件查询【带参数】 **/ //Query query = session.createQuery("from Customer where name = ?"); //query.setParameter(0, "小李");//设置第一个?参数为"小李"【下标从0开始】 /** HQL分页查询操作 **/ Query query = session.createQuery("from Customer"); //查询前3条记录 query.setFirstResult(0); query.setMaxResults(3); List<Customer> list = query.list(); for(Customer customer:list){ System.out.println(customer); } //6事务提交 tx.commit(); //7释放资源 session.close(); } @Test /**** * Criteria对象------>QBC条件查询操作 */ public void demo4(){ //1通过工具类获取Session Session session = HibernateUtils.openSession(); //2开启事务 Transaction tx = session.beginTransaction(); //5操作 //条件查询操作 /** QBC查询所有操作,简单查询操作 **/ //Criteria creteria = session.createCriteria(Customer.class); /** QBC查询操作,条件查询操作 **/ //Criteria creteria = session.createCriteria(Customer.class); //creteria.add(Restrictions.eq("name", "小王"));//设置条件name=小王 /** QBC查询操作,分页查询操作 **/ Criteria creteria = session.createCriteria(Customer.class); creteria.setFirstResult(0); creteria.setMaxResults(3); List<Customer> list = creteria.list(); for(Customer customer:list){ System.out.println(customer); } //6事务提交 tx.commit(); //7释放资源 session.close(); } @Test /**** * 演示一个错误【经常发生】customer1的ID=customer2的ID */ public void demo5(){ //1通过工具类获取Session Session session = HibernateUtils.openSession(); //2开启事务 Transaction tx = session.beginTransaction(); //5操作 //customer1的ID不能=customer2的ID Customer customer1 = (Customer) session.get(Customer.class, 6); Customer customer2 = new Customer(); customer2.setId(6); customer2.setName("张飞"); session.update(customer2); //6事务提交 tx.commit(); //7释放资源 session.close(); } @Test /**** * 演示持久化类为final情况 */ public void demo6(){ //1通过工具类获取Session Session session = HibernateUtils.openSession(); //2开启事务 Transaction tx = session.beginTransaction(); //5操作 //将Customer用final修饰 /** customer的对象为真实对象,而不是代理对象了 **/ Customer customer = (Customer) session.load(Customer.class, 6); //6事务提交 tx.commit(); //7释放资源 session.close(); }}
0 0
- 深入理解hibernate中的API
- 深入理解Hibernate
- Hibernate--深入理解hibernate日志
- Hibernate核心API(理解)
- hibernate api理解学习
- 深入理解 spark api --- Aggregate
- Hibernate中的常用API
- hibernate深入理解-点滴记录
- 深入理解Hibernate的配置文件
- Spring4深入理解----整合Hibernate
- 深入理解MIDP低层用户界面API
- 深入理解MIDP低层用户界面API
- 深入理解 Java Collections API (I)
- 深入理解 Java Collections API (II)
- 深入理解JavaScript中的函数
- 深入理解VC中的消息
- 深入理解VC中的消息
- 深入理解JavaScript中的函数
- mysql数据库锁定机制
- A1056. Mice and Rice (25)
- 时间序列分析(3)R语言-最基础的回归模型
- 配置声明(Configuration Declaration)
- Android默认开机横屏总结
- 深入理解hibernate中的API
- hashmap 中hash函数h & (length-1)详解
- Kaggle学习(一):numpy基本函数使用
- 多工程目录的实现
- 搭建WebSocket服务器与客户端
- c++智能指针(smart pointer)详解
- java 解决图片防盗链
- PAT 1016 Phone Bills
- shell 脚本教程 入门级