推土机抛出lazyInitializationException [英] Dozer throwing lazyInitializationException

查看:84
本文介绍了推土机抛出lazyInitializationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的POJO映射到DTO.我没有任何自定义字段映射器,因为DTO中的所有字段都与POJO中的所有字段相同.但是我的POJO涉及多个级别的映射.我的问题是,当我尝试将POJO的内容复制到DTO时,我遇到了LazyInitializationException.这是引发异常的代码.

I'm trying to map my POJO to a DTO. I do not have any custom field mappers as all my fields in my DTO are same as in my POJO. But my POJO has multiple level of mapping involved. My problem is when i'm trying to copy the contents of my POJO to the DTO, i'm getting LazyInitializationException. Here is the code where the exception is thrown.

public TestInfoDTO startTest(long testId)
{
    TestInfo info = testDAO.startTest(testId);

    Mapper mapper = new DozerBeanMapper();
    try
    {
        // Exception thrown here.
        infoDTO = mapper.map(info, TestInfoDTO.class);

    } catch(Exception e) {
        LOGGER.info("Exception Thrown While Mapping POJO to DTO");
        e.printStackTrace();
    }
    return infoDTO;
}

这是我的POJO.

@Entity
@Table(name = "TEST_INFO")
public class TestInfo implements Serializable,IAuditLog
{
private static final long serialVersionUID = 1L;

private long test_ID;
private String test_name;
private Date creation_date;
private String instructions;
private TestPaperInfo testPaperInfo;
private List<TestResults> testResults;
private List<TestResponse> testResponses;
private List<TestUtility> testUtility;

//Getters and setters with hibernate annotations.
}

这是我的DTO

public class TestInfoDTO 
{
private long test_ID;
private String test_name;
private Date creation_date;
private String instructions;
private TestPaperInfo testPaperInfo;
private List<TestResults> testResults;
private List<TestResponse> testResponses;
private List<TestUtility> testUtility;

//Getters and Setters.
}

在上面的POJO中,TestPaperInfo具有另一个类的Collection,该类又具有问题的集合,而每个问题也具有Answers的集合.它们都是使用JPA注释映射的.

In the above POJO, the TestPaperInfo has a Collection of another class which in-turn has a collection of questions and each question has a collection of Answers. They are all mapped using JPA annotations.

我已经检查了我从DAO获取的"info"对象的内容,并且一切都存在.但是,当我尝试将其复制到DTO("infoDTO"对象)时,会引发LazyInitializationException. 这是我第一次使用DTO和推土机,因此有人可以建议我是否缺少某些东西?还是问题出在哪里?预先感谢.

I have checked the contents of the "info" object that i'm getting from the DAO and everything is present. But when I'm trying to copy it to the DTO ("infoDTO" object) LazyInitializationException is thrown. This is the first time I'm using a DTO and Dozer so could anyone suggest if I'm missing something? Or what the problem is? Thanks in advance.

推荐答案

我猜testDAO是@Transactionnal.这意味着一旦testDAO.startTest完成,事务就关闭了.

I guess testDAO is @Transactionnal. This means once testDAO.startTest has finished, the transaction is closed.

那是除非交易是事先开始的.在主startTest函数上放置一个@Transactionnal批注.这样,当推土机映射到DTO并可以访问代理模型TestInfo时,事务仍将打开.

That is unless the transaction is started before hand. Place a @Transactionnal annotation on your main startTest function. This way the transaction will still be open when dozer maps to a DTO and accessing proxy model TestInfo will be possible.

这篇关于推土机抛出lazyInitializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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