hibernate会话的get()和load()方法在获取方面有什么区别?

sar*_*sar 7 java hibernate

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

 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(); 
Run Code Online (Sandbox Code Playgroud)

}

我得到了以下输出

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
Run Code Online (Sandbox Code Playgroud)

从ouput可以看出get方法没有命中数据库.它的行为与load()方法类似.任何人都可以告诉我这种行为是否正确.

Eng*_*eer 6

正如T Mishra 在此所述:

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

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

  3. 虽然此对象的状态从持久变为分离

可以通过两种方式检索实体.

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

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

有关详细信息,请单击此链接