休眠会话的get()和load()方法在获取方面有什么区别? [英] What is difference between get() and load() method of hibernate session with respect to fetching?

查看:82
本文介绍了休眠会话的get()和load()方法在获取方面有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

get()和load()方法有什么区别?关于数据获取方法

  public static void main(String [] args){
SessionFactory factory = new Configuration ().configure()buildSessionFactory();
会话会话= factory.openSession();
Transaction tx = null;
tx = session.beginTransaction();
System.out.println(第一次调用加载方法);
Account acc =
(Account)session.load(Account.class,180);
System.out.println(bal+ acc.getBalance());

System.out.println(第二次调用加载方法);
账户acc1 =(账户)session.load(Account.class,180);
System.out.println(bal+ acc1.getBalance());


System.out.println(第一次调用get方法);
帐户acc2 =(帐户)session.get(Account.class,accId);

System.out.println(bal+ acc2.getBalance());

System.out.println(第二次调用get方法);

帐户acc2 =(帐户)session.get(Account.class,accId);

System.out.println(bal+ acc2.getBalance());


tx.commit();

session.close();

}



/ p>

 第一次调用加载方法
Hibernate:
/ * load com.abcd.Account * / select
account0_.ACCOUNTID as ACCOUNTID1_0_,
account0_.ACCOUNTTYPE as ACCOUNTT2_1_0_,
account0_.CREATIONDATE as CREATION3_1_0_,
account0_.BALANCE as BALANCE1_0_
from
a。 MYACCOUNT account0_
其中
account0_.ACCOUNTID =?
bal3000.0
第二次调用加载方法
bal3000.0
第一次调用get方法
bal3000.0
第二次调用get方法
bal3000.0

从输出中可以明显看出get方法没有打到数据库。像load()方法一样。任何人都可以告诉我这种行为是否正确。

解决方案

正如T Mishra所述



  1. 默认情况下,hibernate创建运行时代理。它将对象加载为代理,除非指定了获取模式或将其设置为false。


  2. 这是因为一旦对象被加载到缓存中,下一个后续调用执行可重复的读取。


  3. 尽管此对象的状态从持续变为分离





  4. load() - 返回带有标识符的代理对象。



    get() - 从数据库返回完整的对象。



    获取更多信息点击链接



What is difference between get () and load() method? with respect to data fetching approach

 public static void main(String[] args) {
    SessionFactory factory= new Configuration().configure().buildSessionFactory();
     Session session = factory.openSession();
     Transaction tx = null;
      tx = session.beginTransaction();
       System.out.println("1 st time calling load method");
        Account acc = 
               (Account)session.load(Account.class, 180); 
             System.out.println("bal"+acc.getBalance());

          System.out.println("2nd   time calling load method");
          Account  acc1=(Account)session.load(Account.class, 180); 
           System.out.println("bal"+acc1.getBalance());


        System.out.println("1 st time calling get method");
     Account acc2= (Account) session.get(Account.class, accId);

      System.out.println("bal"+acc2.getBalance());    

      System.out.println("2 st time calling get method");

     Account  acc2= (Account) session.get(Account.class, accId);

    System.out.println("bal"+acc2.getBalance());


     tx.commit();

   session.close(); 

}

I got following output

1 st time calling load method
Hibernate: 
/* load com.abcd.Account */ select
    account0_.ACCOUNTID as ACCOUNTID1_0_,
    account0_.ACCOUNTTYPE as ACCOUNTT2_1_0_,
    account0_.CREATIONDATE as CREATION3_1_0_,
    account0_.BALANCE as BALANCE1_0_ 
from
    a.MYACCOUNT account0_ 
where
    account0_.ACCOUNTID=?
bal3000.0
2nd   time calling load method
bal3000.0
1 st time calling get method
bal3000.0
2 st time calling get method
bal3000.0

From ouput it is clear that get method did not hit database.It behaves like load() method. Could any one tell me is this behavior correct.

解决方案

As T Mishra states here:

  1. By default, hibernate creates run-time proxies. It loads the objects as a proxy unless a fetch mode is specified or set to false.

  2. That's because once the object is loaded in cache, the next subsequent calls perform repeatable read.

  3. Although the state of this object changes from persistent to detached

The entity can be retrieved in 2 ways.

load() - returns the proxy object with an identifier.

get() - returns the complete object from database.

for more details click this link

这篇关于休眠会话的get()和load()方法在获取方面有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆