LazyInitialisationException - 无法懒惰地初始化角色集合

时间:2017-12-07 10:43:44

标签: java database hibernate

此代码的目的是允许用户检查ID;在这种情况下,它将是用于检查特定运动的比赛的ID,以便查看相关比赛及其详细信息。这样做的目的是告知用户,如果他们输入了错误的ID,我只想在SOAP请求中生成一条消息,通知输入的competitionID不正确。当我调试我的代码时,我遇到了一个错误" LazyInitialisationException:懒得初始化一个角色集合",任何想法?

public void CompetitionIdChecker(Set<RestrictionFailure> failures, CompetitionSetup competition) {
    Long compId21 = Long.valueOf(competition.getCompetitionId());
    try {
        Node competitionNode = nodeDao.findById(competition.getCompetitionId());
        Set<Node> childNodes = competitionNode.getChildNodes();
        Iterator<Node> nodeItr = childNodes.iterator();
        Node feedNode = nodeItr.next();
        Feed incidentFeed = competition.getIncidentProvider();
        Node Node = null;
        if (incidentFeed.name().equalsIgnoreCase(feedNode.getSys().getCode())) {
            Node = feedNode.findLinkedNode(new SystemKey(incidentFeed.name()));
        }
        if (Node != null) {
            // as node is set as final level in nodes hierarchy
            if (Node.getChildNodes().size() > 0) {
                System.out.println("it is not valid");
                failures.add(new RestrictionFailure("Competition ID does not exist in Nodes table"));
            } else {
                System.out.println("compId is valid");
            }

        }
    } catch (Exception e) {
        LOGGER.error("Error getting competition id", e);
        // rollback(tx);
    }
}

1 个答案:

答案 0 :(得分:0)

多对一关系的默认映射设置为延迟加载。这意味着在获取父对象时不会查询子节点。这是有道理的,否则您将查询父对象的所有关系,即使您永远不会使用它。因此,当您访问此类子对象时,您会收到LazyLoadingException。 要解决此问题,您可以将映射设置为非惰性,或者创建一个获取子项的查询。请看一下fetch join。 https://docs.jboss.org/hibernate/orm/3.5/reference/de-DE/html/queryhql.html#queryhql-joins