休眠会话的get()和load()方法在获取方面有什么区别? [英] What is difference between get() and load() method of hibernate session with respect to fetching?
问题描述
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所述:
默认情况下,hibernate创建运行时代理。它将对象加载为代理,除非指定了获取模式或将其设置为false。
这是因为一旦对象被加载到缓存中,下一个后续调用执行可重复的读取。
- 尽管此对象的状态从持续变为分离
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:
By default, hibernate creates run-time proxies. It loads the objects as a proxy unless a fetch mode is specified or set to false.
That's because once the object is loaded in cache, the next subsequent calls perform repeatable read.
- 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屋!