一尘不染

在没有活动会话的情况下访问对象的字段

hibernate

我正在使用Hibernate 4.0.1.Final。如果该对象已通过org.hibernate.Session.load(Class
clazz,Serializable id)方法加载并且没有活动的会话,是否可以访问该对象的字段?

我使用此代码通过id访问对象…

protected Object find(Class clazz, Serializable id) {
    Object obj = null;
    try {
        startOperation();
        obj = session.load(clazz, id);
        tx.commit();
    } catch (HibernateException e) {
        handleException(e);
    } finally {
        session.close();
    }
    return obj;
}

但是如果我的对象没有活动的会话,例如这段代码……

final Organization foundOrg = orgDao.findById(org.getOrganizationId());
System.out.println(foundOrg.getName());

我在“ System.out”行上收到此错误…

org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:149)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:195)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
    at org.myco.myproject.orgsclient.model.Organization_$$_javassist_0.getName(Organization_$$_javassist_0.java)
    at org.myco.myproject.orgsclient.dao.OrganizationDAOTest.testInsertSchool(OrganizationDAOTest.java:43)
    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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

阅读 177

收藏
2020-06-20

共1个答案

一尘不染

使用session.get(Class, Serializable)代替session.load(Class, Serializable)

加载会将代理返回给该对象,当您首次访问代理上的属性时,将从数据库中加载该对象。如果使用session.get(),则可以保证会返回完全初始化的对象。

加载和获取之间的另一个区别是找不到对象的结果。Get将返回null,而load将始终返回代理。访问属性时,此代理将引发异常。

2020-06-20