简体   繁体   English

从投影列表获取子实体时出现间歇性惰性异常

[英]Intermittent Lazy exception while fetching the child entities from the projectionlist

In my spring project, I'm getting lazy intialization exception intermittently when i try to fetch the child entity from the object got via projection list in dao layer 在我的春季项目中,当我尝试从通过dao层中的投影列表获取的对象中获取子实体时,间歇性地出现了惰性初始化异常

    public class TestMapping extends PersistentEntity {

    private static final long serialVersionUID = 1L;

    private TestModel testModel = new TestModel();

    private String testMappingString;

    private String testModelCategory;

    private Boolean active;

    }

<hibernate-mapping>
    <class name="com.domain.TestModel"
        dynamic-insert="true" dynamic-update="true"
        table="ref_manufacturermodel">

        <id name="id" column="testmodelid" type="long"
            unsaved-value="-1">
            <generator class="seqhilo">
                <param name="max_lo">1</param>
                <param name="sequence">
                    testmodel_seq
                </param>
            </generator>
        </id>

        <version name="version" column="version"
            type="java.lang.Integer" unsaved-value="null" />

        <component name="auditInfo"
            class="com.domain.AuditInfo">

            <many-to-one name="createdBy"
                class="com.domain.User" cascade="none" outer-join="auto"
                update="false" insert="true" 
                column="createdby" not-null="true" />

            <property name="createdDate" type="java.util.Date"
                update="false" insert="true" column="createddate" not-null="true" />

            <property name="lastUpdatedDate" type="java.util.Date"
                update="true" insert="true" column="lastupdateddate" />

            <many-to-one name="updatedBy"
                class="com.domain.User" cascade="none" outer-join="auto"
                update="true" insert="true" 
                column="updatedby" />

        </component>

        <property name="modelNumber" type="java.lang.String"
            update="true" insert="true" column="modelnumber" not-null="true" />

        <property name="modelTitle"
            type="com.hibernate.userType.LabelUserType" update="true"
            insert="true" column="title" />

        <property name="modelCode" type="java.lang.String"
          update="true" insert="true" column="modelcode"/>

        <property name="active" type="java.lang.Boolean" not-null="true"
            update="true" insert="true" column="active" />

        <many-to-one name="model" class="com.domain.Model"
            cascade="none" update="true" insert="true" foreign-key="fk_model_modelid"
            column="modelid" />  

        .................................................
        .................................................
        .................................................

    </class>    

</hibernate-mapping>


    public class TestModel extends PersistentEntity {


     private Model model;
     ........
     ........//Number of child objects

    }


    public interface TestModelDAO extends DAO<TestModel, Long> {
       public List<TestModel> findTestModelByMapping(String testString, String tester,String testModelCategory);
    }

    public class TestModelDaoImpl extends BaseDAOImpl<TestModel, Long> implements TestModelDAO {
    @Override
    public List<TestModel> findTestModelByMapping(String testString, String tester,String testModelCategory) {
        DetachedCriteria dc = DetachedCriteria.forClass(TestMapping.class);
        dc.setProjection(Projections.property("testModel"));
        dc.createAlias("testModel", "tm");
        dc.createAlias("tm.categorytester", "cm");
        dc.createAlias("cm.category", "category");
        dc.add(Restrictions.sqlRestriction("lower(testString) in (?)", testString.toLowerCase(),
                Hibernate.STRING));
        dc.add(Restrictions.eq("testModelCategory", testModelCategory));
        return getHibernateTemplate().findByCriteria(dc);
    }
    }


    public interface TestMappingService {
        public ManufacturerModel getMappedModels();
    }


    @Service("testMappingService")
    public class TestMappingServiceImpl implements TestMappingService {
       public ManufacturerModel getMappedModels() {
        ......................
        ......................
        List<TestModel> modelList=testModelDao.findTestModelByMapping("abc", "test1", "category1");
        TestModel testModel = modelList.size()>0?modelList.get(0):null;
        Workflow workflow = testModel.getModel().getWorkflow;
        **// When i put debugger point on testmodel object it shows me the com.sun.jdi.InvocationException on the child entity *Model*. So as soon as getWorkflow is called, lazy exception is thrown. ** 
       }
    }
  1. In one of my service class, i am getting the List The above method returns me the list of TestModel and when i try to access the child objects of the TestModel then i got the lazy initialization exception in the logs. 在我的服务类之一中,我正在获取List上面的方法向我返回了TestModel的列表,当我尝试访问TestModel的子对象时,我在日志中得到了惰性初始化异常。

  2. When i debug the code and mouse over the TestModel object, the debugger popup displays the com.sun.jdi.InvocationException for child objects of the TestModel object. 当我调试代码并将鼠标悬停在TestModel对象上时,调试器弹出窗口将显示TestModel对象的子对象的com.sun.jdi.InvocationException

  3. I don't get this exception every time. 我没有每次都遇到这个例外。 Some times its displaying 有时会显示

Not sure why we are seeing such a weired behavior. 不知道为什么我们会看到这种奇怪的行为。 Why the lazy exception is displaying for the child oject? 为什么为子对象显示惰性异常? Any suggestions? 有什么建议么?

The default thing for Hibernate to do is being lazy, and not load child objects, but only do that when needed. Hibernate的默认操作是懒惰,不加载子对象,而是仅在需要时才这样做。

But for this to work the parent object has to be in persisted state, with an active persistent context. 但是,要使其正常工作,父对象必须处于具有活动持久上下文的持久状态。

You are making a detached query, and therefore the parent is in detached state. 您正在进行分离查询,因此父级处于分离状态。

You need to have get a Criteria from the Session object. 您需要从Session对象获取一个Criteria。

Is your method ,which is manipulating the objects returned from DAO, running in a transaction. 是您的方法,它处理在事务中运行的从DAO返回的对象。 If not you can try using @Transactional on the method of getMappedModels() TestMappingService interface , see if that solves your problem. 如果不是,您可以尝试在getMappedModels()TestMappingService接口的方法上使用@Transactional,看看是否可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM