在hibernate中了解session.get与session.load方法 [英] understanding session.get vs session.load method in hibernate

查看:95
本文介绍了在hibernate中了解session.get与session.load方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解加载和获取之间的区别。当我给session.load时,下面的一段代码不起作用。它给出空指针异常。但是,当我使用session.get()时,它也起作用。
$ b

  public Employee getEmployee(final String id){
HibernateCallback回调=新HibernateCallback(){
公共对象doInHibernate(会话的会话)
抛出HibernateException的,{的SQLException
//返回(员工)session.load(Employee.class,身份证);不工作
return(Employee)session.get(Employee.class,id); //它可以工作
}
};
return(Employee)hibernateTemplate.execute(callback);
}

我也想了解Session对象是如何传递给doInHibernate的。
什么时候会话开始,什么时候结束?



堆栈跟踪如下



'pre> 异常在线程org.hibernate.tuple.AbstractEntityTuplizer.createProxy 主 显示java.lang.NullPointerException
(AbstractEntityTuplizer.java:372)
org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:3121)
at org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:232)
at org。 hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:173)
在org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
在org.hibernate.impl。 SessionImpl.fireLoad(SessionImpl.java:862)
在org.hibernate.impl.SessionImpl.load(SessionImpl.java: 781)
在org.hibernate.impl.SessionImpl.load(SessionImpl.java:774)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
。在sun.reflect.NativeMethodAccessorImpl。调用(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.hibernate3.HibernateTemplate $ CloseSuppressingInvocationHandler.invoke(HibernateTemplate.java:1282)
at $ Proxy0.load(Unknown Source)
at hibernate.EmployeeDao $ 1.doInHibernate(EmployeeDao。 java:25)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:339)在hibernate.EmployeeDao.getEmployee(EmployeeDao.java:29)

。在hibernate.SpringHibernateTest.main(SpringHibernateTest.java:26)

解决方案

我无法理解负载和得到

主要区别在于:如果load()无法在缓存或数据库中找到对象,抛出异常。 load()方法永远不会返回null。如果找不到对象,则get()方法返回
null。
其他区别在于load()方法可能会返回一个代理而不是真实的实例,但get()永远不会返回代理。

 当我给session.load时,下面的一段代码不起作用。它给出空指针异常。但是,当我使用session.get()时,它也可以工作。 

如果找不到对象,load方法会抛出异常,但不会被获取。简单



编辑:
详细说明,


  1. 当调用get()方法时,它将直接命中数据库,获取结果并返回。如果找不到匹配的字段,它会很乐意返回null。

  2. 但是,当load()执行时,首先它会搜索缓存中的所需对象。如果找到了,一切都很好。但是如果在缓存中找不到对象,load()方法将返回一个代理。您可以将此代理视为数据库查询执行的快捷方式。请记住,没有数据库命中。现在,当你实际访问对象时,代理将被跟踪并且数据库命中。

让我们考虑简单例子。

  User user =(User)session.load(User.class,new Long(1)); // Line 1 
System.out.println(user.getPassword()); //第2行

如果用户对象与主键1在会话中不可用,则load()方法将为第1行上的数据库设置代理。现在,当调用'用户'对象的实际值时,即第2行时,代理将被追踪和数据库将被击中。



希望这会有所帮助。


I am not able to understand the difference between load and get . the following piece of code doesn't work when i give session.load. It gives null pointer exception. But same does work when i am using session.get() .

public Employee getEmployee(final String id){        
        HibernateCallback callback = new HibernateCallback() {
            public Object doInHibernate(Session session) 
                throws HibernateException,SQLException {
                //return (Employee)session.load(Employee.class, id);   doesn't work
                  return (Employee)session.get(Employee.class, id);    //it works
            }
        };        
        return (Employee)hibernateTemplate.execute(callback);
    }

I also want to understand how Session object is passed to doInHibernate.?
when does session starts and when it ends?

Stack trace is as follows

Exception in thread "main" java.lang.NullPointerException
    at org.hibernate.tuple.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:372)
    at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:3121)
    at org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:232)
    at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:173)
    at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
    at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:862)
    at org.hibernate.impl.SessionImpl.load(SessionImpl.java:781)
    at org.hibernate.impl.SessionImpl.load(SessionImpl.java:774)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.orm.hibernate3.HibernateTemplate$CloseSuppressingInvocationHandler.invoke(HibernateTemplate.java:1282)
    at $Proxy0.load(Unknown Source)
    at hibernate.EmployeeDao$1.doInHibernate(EmployeeDao.java:25)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
    at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:339)
    at hibernate.EmployeeDao.getEmployee(EmployeeDao.java:29)
    at hibernate.SpringHibernateTest.main(SpringHibernateTest.java:26)

解决方案

I am not able to understand the difference between load and get 

The main difference is: if load() can’t find the object in the cache or database, an exception is thrown. The load() method never returns null. The get() method returns null if the object can’t be found. Other difference is that the load() method may return a proxy instead of a real instance but get() never does return proxy.

the following piece of code doesn't work when i give session.load. It gives null pointer exception. But same does work when i am using session.get() .

If object is not found, load method will throw exception but get won't.Simple

Edit: To elaborate the things,

  1. When get() method is called, it will directly hit the database, fetch the result and return. If no matching fields are found, it will gladly return null.

  2. But when load() executes, firstly it will search the cache for required object. If found, all is well. But if object is not found in cache, load() method will return a proxy. You can consider this proxy as a shortcut for database query execution. Remember, no database hit is made yet. Now when you actually access the object the proxy will be traced and database hit will be made.

Lets consider a simple example.

User user=(User)session.load(User.class, new Long(1));//Line 1
System.out.println(user.getPassword());//Line 2

If User object with primary key 1 is not available in session, the load() method will set a proxy for the database at Line 1. Now when actual value of the 'user' object is called, i.e. line 2, the proxy will be traced and the database will be hit.

Hope this will help.

这篇关于在hibernate中了解session.get与session.load方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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