欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[Hibernate单表操作] 单表操作CRUD实例

程序员文章站 2022-07-12 18:13:23
...
一 常用方法
save
update
delete
get/load(查询单个记录)
 
二 测试代码
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.util.Date;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
//测试类
public class StudentsTest {
    
    private SessionFactory sessionFactory;
    private Session session;
    private Transaction transaction;
    @Before
    public void init(){
        // 创建配置对象   
        Configuration config = new Configuration().configure();
        //config.addClass(Students.class);
        // 创建服务注册对象
        ServiceRegistry serviceRegistery = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
        // 创建会话工厂对象
        sessionFactory = config.buildSessionFactory(serviceRegistery);
        // 创建会话对象
        session = sessionFactory.openSession();
        // 开启事务
        transaction = session.beginTransaction();
    }
    
    @After
    public void destory(){
        // 提交事务
        transaction.commit();
        // 关闭会话
        session.close();
        // 关闭会话工厂
        sessionFactory.close();
    }
    
    @Test
    public void testSaveStudents(){
        //Students s= new Students(1,"张三丰","男",new Date(),"武当山");
        Students s= new Students();
        s.setSname("张三丰");
        s.setGender("男");
        s.setBirthday(new Date());
        //s.setAddress("武当山");
        Address address = new Address("7232","125674323","湖北");
        s.setAddress(address);
        session.save(s);
    }
    
    @Test
    public void testGetStudents()
    {
        Students s =(Students)session.get(Students.class,1);
        System.out.println(s);
    }
    
    @Test
    public void testLoadStudents()
    {
        Students s =(Students)session.load(Students.class,1);
        System.out.println(s);
    }
    
    @Test
    public void testUpdateStudents()
    {
        Students s =(Students)session.load(Students.class,1);
        s.setGender("女");
        session.update(s);
    }
    
    @Test
    public void testDeleteStudents()
    {
        Students s =(Students)session.load(Students.class,1);
        session.delete(s);
    }
    
}
 
三 测试结果
1 get方法测试结果如下
Students [sid=1, sname=张三丰, gender=男, birthday=2017-10-02 09:36:35.0, address=Address@76fe8d1b]
 
2 load方法测试结果如下
Students [sid=1, sname=张三丰, gender=男, birthday=2017-10-02 09:36:35.0, address=Address@59e9c94]
 
3 update方法测试结果如下

[Hibernate单表操作] 单表操作CRUD实例
            
    
    博客分类: Hibernate Hibernate单表操作CRUD 
 
 
4 delete方法测试结果如下

[Hibernate单表操作] 单表操作CRUD实例
            
    
    博客分类: Hibernate Hibernate单表操作CRUD 
 
 
四 get与load的区别

[Hibernate单表操作] 单表操作CRUD实例
            
    
    博客分类: Hibernate Hibernate单表操作CRUD 
 

[Hibernate单表操作] 单表操作CRUD实例
            
    
    博客分类: Hibernate Hibernate单表操作CRUD 
 
1 测试1:是否立即发出SQL语句
1.1 测试代码:
        @Test
        public void testGetStudents()
        {
                Students s =(Students)session.get(Students.class,1);
                //System.out.println(s);
        }
        
        @Test
        public void testLoadStudents()
        {
                Students s =(Students)session.load(Students.class,1);
                //System.out.println(s);
        }
1.2 测试结果:
testGetStudents会在调试窗口输出下面语句
Hibernate:
    select
        students0_.SID as SID1_0_0_,
        students0_.SNAME as SNAME2_0_0_,
        students0_.GENDER as GENDER3_0_0_,
        students0_.BIRTHDAY as BIRTHDAY4_0_0_,
        students0_.PICTURE as PICTURE5_0_0_,
        students0_.POSTCODE as POSTCODE6_0_0_,
        students0_.PHOTO as PHOTO7_0_0_,
        students0_.ADDRESS as ADDRESS8_0_0_
    from
        STUDENTS students0_
    where
        students0_.SID=?
testLoadStudents不会在调试窗口输出结果
1.3 测试说明:
testLoadStudents只有在用的时候才发出SQL语句,是一种懒加载。
 
2 测试2:get返回对象本身,load返回代理对象
2.1 测试代码:
        @Test
        public void testGetStudents()
        {
                Students s =(Students)session.get(Students.class,1);
                System.out.println(s.getClass().getName());
                //System.out.println(s);
        }
        
        @Test
        public void testLoadStudents()
        {
                Students s =(Students)session.load(Students.class,1);
                System.out.println(s.getClass().getName());
                //System.out.println(s);
        }
2.2 测试结果:
get返回结果:
Students
load返回结果:
Students_$$_javassist_0
 
3 测试3:查询结果为空时区别
3.1 测试代码:
        @Test
        public void testGetStudents()
        {
                Students s =(Students)session.get(Students.class,100);
                //System.out.println(s.getClass().getName());
                System.out.println(s);
        }
        
        @Test
        public void testLoadStudents()
        {
                Students s =(Students)session.load(Students.class,100);
                //System.out.println(s.getClass().getName());
                System.out.println(s);
        }
3.2 测试结果:
get测试结果:
null
load测试结果:

 

抛出异常
  • [Hibernate单表操作] 单表操作CRUD实例
            
    
    博客分类: Hibernate Hibernate单表操作CRUD 
  • 大小: 22.5 KB
  • [Hibernate单表操作] 单表操作CRUD实例
            
    
    博客分类: Hibernate Hibernate单表操作CRUD 
  • 大小: 21.2 KB
  • [Hibernate单表操作] 单表操作CRUD实例
            
    
    博客分类: Hibernate Hibernate单表操作CRUD 
  • 大小: 116.2 KB
  • [Hibernate单表操作] 单表操作CRUD实例
            
    
    博客分类: Hibernate Hibernate单表操作CRUD 
  • 大小: 53.4 KB