2

我正在尝试强制 HQL 急切加载所有 Lazy 属性。使用 FETCH 给了我“多袋”异常,我无法将其更改为 Set,因为它们在其他地方被引用。我尝试了其他映射注释,但没有运气。“FETCH ALL PROPERTIES”被忽略,没有加载任何内容。我一直不明白它的真正作用。显然,当 JSF 尝试读取列表时,我得到“无法初始化惰性列表。连接关闭”

我正在使用休眠 4.0。所以这是我的 HQL 查询:

Select r from Responsavel r FETCH ALL PROPERTIES 
where r.unidadeEmpresarial.id = :idUnidade

这是我的模型课:

public class Responsavel {

@Id
@SequenceGenerator(name = "SQ_RESPONSAVEL_GENERATOR", sequenceName = "SE_RESPONSAVEL", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SQ_RESPONSAVEL_GENERATOR")
@Column(name = "SQ_RESPONSAVEL")
private Long id;

@Temporal(TemporalType.DATE)
@Column(name = "DT_FIM")
private Date dataFim;

@NotNull
@Temporal(TemporalType.DATE)
@Column(name = "DT_INICIO")
private Date dataInicio;

@Temporal(TemporalType.DATE)
@Column(name = "DT_NASCIMENTO")
private Date dataNascimento;

@Type(type = "org.hibernate.type.NumericBooleanType")
@Column(name = "IN_PODER_ADM_REPRES")
private Boolean temPoderAdministrativo;

@Column(name = "NO_RESPONSAVEL")
private String nome;

@Column(name = "NU_CPF_CNPJ_RESPONSAVEL")
private String numeroCpfCnpj;

@NotNull
@Enumerated(EnumType.STRING)
@Column(name = "TP_PESSOA")
private String tipoPessoa;

@ManyToOne
@JoinColumn(name = "SQ_PAIS")
private Pais pais;

@Column(name = "VL_CAPITAL_SOCIAL")
private BigDecimal valorCapitalSocial;

@Column(name = "PC_CAPITAL_SOCIAL")
private BigDecimal percentualCapitalSocial;

@ManyToOne
@JoinColumn(name = "SQ_UNIDADE_EMPRESARIAL")
private UnidadeEmpresarial unidadeEmpresarial;

@OneToMany(mappedBy = "responsavel", fetch = FetchType.LAZY)
@Fetch(FetchMode.SUBSELECT)
private Set<ResponsavelQualificacaoResponsavel> listaResponsavelQualificacaoResponsavel;

@OneToMany(mappedBy = "responsavel")
private List<Representante> representantes;

@OneToMany(mappedBy = "responsavel")
private List<ContatoResponsavel> contatoResponsavel;

@OneToMany(mappedBy = "responsavel", cascade = CascadeType.ALL)
private List<EnderecoResponsavel> enderecoResponsavel;

}

谢谢

4

1 回答 1

2

如果您遇到多包异常,那是因为 Hibernate 无法一次获取所有集合。您应该选择一个您希望使用基本查询选择的集合并使用连接来完成,然后使用 Hibernate.initialize 获取您需要的其余集合。

所以它看起来像这样(我不选择,因为它不是必需的):

from Responsavel r 
join fetch pais p
join fetch unidadeEmpresarial u
join fetch representantes s
where r.unidadeEmpresarial.id = :idUnidade

您可以尝试包含哪些内容。如果您希望从 Hibernate 会话中分离以使用前端或服务类中的集合,则剩下的需要初始化。

Hibernate.initialize(listaResponsavelQualificacaoResponsavel);
Hibernate.initialize(contatoResponsavel);
Hibernate.initialize(enderecoResponsavel);

如果您将保留会话,则无需初始化,它们将按需初始化(尽管这可能会导致性能问题,具体取决于您的用例)。

于 2013-07-29T18:22:27.177 回答