【问题标题】:LazyInitialisationException - failed to lazily initialise a collection of roleLazyInitialisationException - 未能延迟初始化角色集合
【发布时间】:2017-12-07 10:43:43
【问题描述】:

此代码的目的是允许用户检查 ID;在这种情况下,这将是一个用于检查特定运动比赛的 ID,以便查看相关比赛及其详细信息。这样做的全部目的是通知用户,如果他们输入了错误的 ID,我只是想在 SOAP 请求中生成一条消息,通知输入的竞争 ID 不正确。当我调试我的代码时,我遇到了一个错误“LazyInitialisationException: failed to lazily initialize a collection of role”,有什么想法吗?

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);
    }
}

【问题讨论】:

    标签: java database hibernate


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-03
      • 2019-08-06
      • 2017-08-16
      • 2011-04-27
      • 2013-12-08
      • 2020-05-10
      • 2017-08-04
      • 1970-01-01
      相关资源
      最近更新 更多