• CoreAPI_Get_Load


    在测update方法之前,我们看读取:get和load。

    load就是从数据库里取一条记录,取到内存里,把一条记录转化成对应的对象。

    测试代码:

    package hjj.lch.hibernate.model;
    
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;
    import org.junit.AfterClass;
    import org.junit.BeforeClass;
    import org.junit.Test;
    
    public class HibernateCoreAPITest {
    
    private static SessionFactory sf = null;
        
        @BeforeClass
        public static void beforeClass(){
            sf = new AnnotationConfiguration().configure().buildSessionFactory();
        }
        
        @Test
        public void testSave() {
            Teacher t = new Teacher(); 
            t.setName("t1");
            t.setTitle("中级");
            
            Session session = sf.getCurrentSession();
            session.beginTransaction();
            session.save(t); // save后,t变成了Persistent状态
            System.out.println(t.getId());
            session.getTransaction().commit(); // commit之后,t变成了Detached状态
            
        }
    
        @Test
        public void testLoad() {
            
            Session session = sf.getCurrentSession();
            session.beginTransaction();
            Teacher t = (Teacher)session.load(Teacher.class,1); 
            System.out.println(t.getName());
            session.getTransaction().commit(); 
        
        }
    
        @AfterClass
        public static void afterClass(){
            sf.close();
        }
    
    }

    运行后台打印结果:

    14:10:57,792  INFO SchemaExport:226 - Running hbm2ddl schema export
    14:10:57,792 DEBUG SchemaExport:242 - import file not found: /import.sql
    14:10:57,792  INFO SchemaExport:251 - exporting generated schema to database
    14:10:57,802 DEBUG SchemaExport:377 -
        drop table if exists Student
    14:10:57,832 DEBUG SchemaExport:377 -
        drop table if exists Teacher
    14:10:57,852 DEBUG SchemaExport:377 -
        create table Student (
            id integer not null,
            name varchar(16) not null,
            age integer,
            primary key (id, name)
        )
    14:10:57,952 DEBUG SchemaExport:377 -
        create table Teacher (
            id integer not null auto_increment,
            name varchar(255),
            title varchar(255),
            primary key (id)
        )
    14:10:58,042  INFO SchemaExport:268 - schema export complete
    Hibernate:
        insert
        into
            Teacher
            (name, title)
        values
            (?, ?)
    1
    Hibernate:
        select
            teacher0_.id as id1_0_,
            teacher0_.name as name1_0_,
            teacher0_.title as title1_0_
        from
            Teacher teacher0_
        where
            teacher0_.id=?
    t1

         /**
         * Load和Get都能把数据从数据库中取出,但是它们两个之间有重要的区别
         * 当我们用session.Get()去拿一个对象的时候,它马上会发出sql语句,然后从数据库里面取出这个数据的值来给它装到这个对象里面去
         * 但是如果用session.Load()拿时,生成的是这个对象的一个代理,这个代理并没有真正的发出sql语句。
         * 这个sql语句是在我们需要拿它里面的一个属性的时候才会发出
         */

  • 相关阅读:
    挂载硬盘,提示 mount: unknown filesystem type 'LVM2_member'的解决方案
    mongo3.4 配置文件 注意事项
    Rsync 传输不需要输入密码
    Robomongo 0.9.0 连接mongo数据库时,提示连接失败 的解决方案
    linux 安装 mongo
    mysql GTID主从复制(主库在线,添加新丛库)
    计算机网络原理精讲第四章--网络层
    Chrome浏览器商店安装的插件保存到本地
    计算机网络原理精讲第三章--链路层
    计算机网络原理精讲第二章--物理层
  • 原文地址:https://www.cnblogs.com/ligui989/p/3463882.html
Copyright © 2020-2023  润新知