Hibernate急切加载不获取所有子行

时间:2015-08-07 19:06:34

标签: java hibernate

Hibernate版本4.3.10

我有父/子关系,如示例中所述。在某些情况下,我们希望在查询提供程序时只返回一行。在这种情况下,我们通过将setMaxResults方法调用为1来限制条件。

SQL转储显示,hibernate进行外连接调用,最终返回多行,但由于在条件上设置了限制,因此只从数据库中读取第一个子行。

    @Entity
    @Table(name = "F_PROVIDER")
    public class Provider {
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "provider", orphanRemoval = true, fetch = FetchType.EAGER)
        private final Set<CredentialFieldDefinition> credentialFieldDefinitionList;
}

@Entity
@Table(name = "F_CRED_FIELDS", uniqueConstraints = { @UniqueConstraint(columnNames = { "name", "provider_id" }) })
public class CredentialFieldDefinition {
@ManyToOne(targetEntity = Provider.class, optional = false)
    @JoinColumn(name = "PROVIDER_ID", nullable = false, unique = false)
    private Provider provider;
}

当我只阅读一行时,我如何说服我将完整的孩子归还给我?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用SUBSELECT获取模式:

@OneToMany(cascade = CascadeType.ALL, mappedBy = "provider", orphanRemoval = true, fetch = FetchType.EAGER)
@Fetch(FetchMode.SUBSELECT)
private final Set<CredentialFieldDefinition> credentialFieldDefinitionList;

这种方式credentialFieldDefinitionList将在单独的查询中初始化。