休眠从对象中延迟获取嵌套列表

Posted

技术标签:

【中文标题】休眠从对象中延迟获取嵌套列表【英文标题】:Hibernate lazy fetch of nested list from object 【发布时间】:2016-09-12 03:31:37 【问题描述】:

我有 2 个实体:Postulacion 有一个 Experiencia 列表。Experiencia 是关系的所有者,它在他的表中有一个列 postulacion_id

当我进行查询以检索Postulacion 时,问题出现了,返回的对象的Experiencia 列表为空(未加载惰性对象)。我不知道如何使休眠列表加载到休眠状态。

我尝试在@Transactional 方法中使用Hibernate.initialize(),但没有结果。

package com.ksbs.eventum.domain;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.data.elasticsearch.annotations.Document;

/**
 * A Postulacion.
 */
@Entity
@Table(name = "postulacion")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "postulacion")
public class Postulacion implements Serializable 

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "estado")
    private String estado;

    @OneToMany(mappedBy = "postulacion")
    //@JsonIgnore
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<Experiencia> experiencias = new HashSet<>();

    @OneToOne
    @JoinColumn(unique = true)
    private PersonalGenerico personalGenerico;

    public Long getId() 
        return id;
    

    public void setId(Long id) 
        this.id = id;
    

    public String getEstado() 
        return estado;
    

    public void setEstado(String estado) 
        this.estado = estado;
    

    public Set<Experiencia> getExperiencias() 
        return experiencias;
    

    public void setExperiencias(Set<Experiencia> experiencias) 
        this.experiencias = experiencias;
    

    public PersonalGenerico getPersonalGenerico() 
        return personalGenerico;
    

    public void setPersonalGenerico(PersonalGenerico personalGenerico) 
        this.personalGenerico = personalGenerico;
    

    @Override
    public boolean equals(Object o) 
        if (this == o) 
            return true;
        
        if (o == null || getClass() != o.getClass()) 
            return false;
        
        Postulacion postulacion = (Postulacion) o;
        if(postulacion.id == null || id == null) 
            return false;
        
        return Objects.equals(id, postulacion.id);
    

    @Override
    public int hashCode() 
        return Objects.hashCode(id);
    

    @Override
    public String toString() 
        return "Postulacion" +
            "id=" + id +
            ", estado='" + estado + "'" +
            '';
    




package com.ksbs.eventum.domain;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.data.elasticsearch.annotations.Document;

import javax.persistence.*;
import javax.validation.constraints.*;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.Objects;

/**
 * A Experiencia.
 */
@Entity
@Table(name = "experiencia")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "experiencia")
public class Experiencia implements Serializable 

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "cargo")
    private String cargo;

    @Column(name = "puesto")
    private String puesto;

    @Column(name = "detalle")
    private String detalle;

    @NotNull
    @Column(name = "fecha_desde", nullable = false)
    private LocalDate fechaDesde;

    @Column(name = "fecha_hasta")
    private LocalDate fechaHasta;

    @ManyToOne
    private Postulacion postulacion;

    public Long getId() 
        return id;
    

    public void setId(Long id) 
        this.id = id;
    

    public String getCargo() 
        return cargo;
    

    public void setCargo(String cargo) 
        this.cargo = cargo;
    

    public String getPuesto() 
        return puesto;
    

    public void setPuesto(String puesto) 
        this.puesto = puesto;
    

    public String getDetalle() 
        return detalle;
    

    public void setDetalle(String detalle) 
        this.detalle = detalle;
    

    public LocalDate getFechaDesde() 
        return fechaDesde;
    

    public void setFechaDesde(LocalDate fechaDesde) 
        this.fechaDesde = fechaDesde;
    

    public LocalDate getFechaHasta() 
        return fechaHasta;
    

    public void setFechaHasta(LocalDate fechaHasta) 
        this.fechaHasta = fechaHasta;
    

    public Postulacion getPostulacion() 
        return postulacion;
    

    public void setPostulacion(Postulacion postulacion) 
        this.postulacion = postulacion;
    

    @Override
    public boolean equals(Object o) 
        if (this == o) 
            return true;
        
        if (o == null || getClass() != o.getClass()) 
            return false;
        
        Experiencia experiencia = (Experiencia) o;
        if(experiencia.id == null || id == null) 
            return false;
        
        return Objects.equals(id, experiencia.id);
    

    @Override
    public int hashCode() 
        return Objects.hashCode(id);
    

    @Override
    public String toString() 
        return "Experiencia" +
            "id=" + id +
            ", cargo='" + cargo + "'" +
            ", puesto='" + puesto + "'" +
            ", detalle='" + detalle + "'" +
            ", fechaDesde='" + fechaDesde + "'" +
            ", fechaHasta='" + fechaHasta + "'" +
            '';
    



/**
 * GET  /postulacions/:id : get the "id" postulacion.
 *
 * @param id the id of the postulacion to retrieve
 * @return the ResponseEntity with status 200 (OK) and with body the postulacion, or with status 404 (Not Found)
 */

@RequestMapping(value = "/postulacions/id",       method = RequestMethod.GET,     produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
@Transactional
public ResponseEntity<Postulacion> getPostulacion(@PathVariable Long id) 

    log.debug("REST request to get Postulacion : ", id);
    Postulacion postulacion = postulacionRepository.findOne(id);
    return Optional.ofNullable(postulacion)
            .map(result -> new ResponseEntity<>(
                    result,
                    HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));

【问题讨论】:

【参考方案1】:

你可以告诉 hibernate 你的列表应该被急切地获取(而不是懒惰的)

@OneToMany(mappedBy = "postulacion", fetch = FetchType.EAGER)
//@JsonIgnore
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Experiencia> experiencias = new HashSet<>();

这将与父实体一起加载实体,但使用它时要小心,不要加载整个图表

编辑:您可能需要在此处添加@JsonIgnore

public class Experiencia implements Serializable 
...
@ManyToOne
@JsonIgnore
private Postulacion postulacion;
...

或者它在尝试序列化为 JSON 时可能会发生一些堆栈溢出错误。在尝试序列化时,您不是已经出现错误,因为它没有立即加载吗?

【讨论】:

JsonIgnore on Postulacion 解决了这个问题。谢谢。【参考方案2】:

也许您可能希望保持延迟加载不变,然后在您的 hql 添加 fetch 类型以进行连接或选择。阅读文档以获取具体示例:14.3. Associations and joins

你甚至可以决定使用实体图,这篇文章应该会有所帮助:JPA 2.1 Entity Graph – Part 1: Named entity graphs

【讨论】:

以上是关于休眠从对象中延迟获取嵌套列表的主要内容,如果未能解决你的问题,请参考以下文章

具有多个关联的休眠获取

使用 FetchType.LAZY 休眠 ManyToOne 不会延迟获取

使用休眠仅从对象中的表中获取选定的列

即使在 HQL 查询中使用 Join Fetch,休眠延迟初始化异常

休眠延迟加载不适用于多对一映射

休眠延迟加载的 JSF2 + Ajax EL 问题