我有2个实体:Postulacion
有一个Experiencia
列表。Experiencia
是该关系的所有者,他的表中有一列postulacion_id
。
当我进行查询以检索Postulacion
时出现问题,返回的对象的列表为Experiencia
为空(未加载惰性对象)。我不知道如何使懒惰列表被hibernate加载。
我尝试在@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));
}
答案 0 :(得分:0)
你可以告诉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时可能会发生一些堆栈溢出错误。在尝试序列化时,您是否已经有错误,因为这不是急切加载的?
答案 1 :(得分:0)
也许您可能希望保留延迟加载,然后在您的hql add fetch类型中进行连接或选择。阅读具体示例的文档:14.3. Associations and joins
您甚至可能决定使用实体图,本文应该有所帮助:JPA 2.1 Entity Graph – Part 1: Named entity graphs