Ленивый выбор - PullRequest
       11

Ленивый выбор

0 голосов
/ 25 октября 2019

Я пытаюсь получить объект A, который также имеет отношения с другим классом объектов B. Но из-за ленивой загрузки A я получаю LazyException и JsonMappingException.

Я попробовал следующие ссылки: Hibernate + Lazy fetch + gson = LazyInitializationException

Hibernate LAZY fetch

@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name = "patient")
public class Patient  implements Serializable {

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

    @Column(name = "first_name", nullable = false, length = 50)
    private String firstName;

    @Column(name = "middle_name", length = 50)
    private String middleName;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "surname_id")
    private Surname surname;
}
@Entity
@Table(name = "surname")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Surname implements Serializable {

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

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

    @Column(name = "status")
    private Character status;

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

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ethnicity_id")
    private Ethnicity ethnicity;
}

Исключение:

The server encountered an unexpected condition that prevented it from fulfilling the request.</p><p><b>Exception</b></p><pre>org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: could not initialize proxy [com.cogent.persistence.model.Ethnicity#1] - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy [com.cogent.persistence.model.Ethnicity#1] - no Session (through reference chain: com.cogent.patientregistration.dto.response.patient.PatientResponseDTO[&quot;patient&quot;]-&gt;com.cogent.persistence.model.Patient[&quot;surname&quot;]-&gt;com.cogent.persistence.model.Surname$HibernateProxy$D7grS1t7[&quot;ethnicity&quot;]-&gt;com.cogent.persistence.model.Ethnicity$HibernateProxy$f67J9iWj[&quot;name&quot;])
    org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:296)
    org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:103)
    org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:290)

Как мне решить эту проблему?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...