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

Hibernate lazy fetch of nested list from object

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

当我进行查询以检索 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));
}

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

@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 时可能会出现堆栈溢出错误。当你尝试序列化时,你不是已经有错误了吗,因为这不是急切加载的?

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

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