我有实体Participant

public class Participant extends AbstractParticipant
{
    ...

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "participant")
    private List<MediaCallParticipant> mediaCallParticipant = new LinkedList<>();
    
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "participant")
    private List<AudioCallParticipant> audioCallParticipant = new LinkedList<>();
}

我在写一个测试.我做的是participantRepository.findOne(...) 但在测试中,我无法获得具有participant.getAudioCallParticipant()participant.getMediaCallParticipant()的LazyLoadException. 我已经编写了代码,这是准备懒惰的异常.但是这些集合总是在实体中加载的. 有没有可能以某种方式手动"卸载"这些Collection ?

推荐答案

您的代码应该是有效的. 如果您需要在测试中执行此操作,请try 以下代码

        // init here another context

        TestTransaction.start();
        participant = participantRepository.findOne(participant);
        getSessionFactory().getStatistics().clear();
        participant.getMediaCallParticipant();

        //assert
        assertThat(getStatistics().getPrepareStatementCount(),is(1L));

Java相关问答推荐

Java取消任务运行Oracle查询通过JDBC—连接中断,因为SQLSTATE(08006),错误代码(17002)IO错误:套接字读取中断

CAMEL 4中的SAXParseException

Spring Boot@Cachebale批注未按预期工作

Com.example.service.QuestionService中的构造函数的参数0需要找不到的类型为';com.example.Dao.QuestionDao;的Bean

Helidon 4和Http API

与不同顺序的组进行匹配,不重复组但分开

Java中将文本拆分为数字或十进制数字和字符串

在学习Spring时,通过构造函数参数0表达了不满意的依赖关系

Instancio未在日志(log)中显示测试失败消息

如何在Jooq中获取临时表列引用?

在Spring Boot应用程序中,server.port=0的默认端口范围是多少?

为什么StandardOpenOption.CREATE不能通过Ubuntu在中小企业上运行?

如何在字节数组中反转UTF-8编码?

处理4.3问题:javax.xml.ind包不存在(&Q;).您可能在学习GitHub教程时遗漏了库.&Q

Android无法在Java代码中调用Kotlin代码,原因是在Companion中使用Kotlin枚举时

使用同步方法中的新线程调用同步方法

Java 21内置http客户端固定运营商线程

ReturnedRect在升级后反转

如何从指定某些字段的父对象创建子对象

为什么 PhantomReference.get() 总是返回 null?