Hibernate lazy fetch of nested list from object
<p>I have 2 entities: <code>Postulacion</code> has a list of <code>Experiencia</code>.<code>Experiencia</code> is the owner of the relationship, it has a column <code>postulacion_id</code> in his table. </p>

<p>The problem comes when I make a query to retrieve a <code>Postulacion</code>, the returned object has the list of <code>Experiencia</code> empty (the lazy objects are not loaded). I don't know how to make lazy list to be loaded by hibernate. </p>

<p>I have tried using <code>Hibernate.initialize()</code> in an @Transactional method, without results. </p>

<pre><code>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&lt;Experiencia&gt; experiencias = new HashSet&lt;&gt;();

    @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&lt;Experiencia&gt; getExperiencias() {
        return experiencias;
    }

    public void setExperiencias(Set&lt;Experiencia&gt; 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&lt;Postulacion&gt; getPostulacion(@PathVariable Long id) 
{
    log.debug("REST request to get Postulacion : {}", id);
    Postulacion postulacion = postulacionRepository.findOne(id);
    return Optional.ofNullable(postulacion)
            .map(result -&gt; new ResponseEntity&lt;&gt;(
                    result,
                    HttpStatus.OK))
            .orElse(new ResponseEntity&lt;&gt;(HttpStatus.NOT_FOUND));
}
</code></pre>