@OneToOne在另一个表中具有反向外键

时间:2019-06-03 09:43:28

标签: hibernate spring-data-jpa one-to-one

当另一个表中只有一个外键时,如何从另一个表中获取对象?

我有以下实体:

public class ViolationFactor extends BaseEntity {

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

    @Column(name = "stopien")
    private float degree;

    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
//  @JsonBackReference
    @OneToOne(mappedBy = "violationFactor", cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
    private IncidentAssessmentFactor incidentAssessmentFactor;
}

public class IncidentAssessmentFactor extends BaseEntity {

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

    @Column(name="czynnik_wybrany")
    private Boolean factorIsSelected;

    @Column(name = "wartosc_wybrana")
    private float value;

//  @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="ocena_naruszenia_wynik_id", updatable=false, insertable=false)
    private IncidentAssessment incidentAssessment;

//  @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
//  @JsonManagedReference
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "czynnik_naruszen_id", unique = true)
    private ViolationFactor violationFactor;
}

表ViolationFactor没有指向IncidentAssessmentFactor的任何外键-它只是一本字典。 IncidentAssessmentFactor与ViolationFactor无关。

如何从IncidentAssessmentFactor中删除IncidentAssessmentFactor属性?我只希望有一个@OneToOne单向绑定。现在我有了循环依赖: 违规因素->突发事件评估因素->违规因素 当执行到DTO的映射时,我得到堆栈溢出错误:

是否可以拥有财产IncidentAssessmentFactor eventAssertsFactor;仅在ViolationFactor实体中?

我的存储库:

public interface ViolationCriterionRepository extends JpaRepository<ViolationCriterion, Long> {

    @Query("select vc from ViolationCriterion vc join vc.violationFactors vf join vf.incidentAssessmentFactor iaf join iaf.incidentAssessment ia where ia.incidentAssessmentId = ?1 group by vc ")
    List<ViolationCriterion> findIncidentAssessmentByIncidentAssessmentId(Long incidentId);
}
  

java.lang.StackOverflowError:在为null   org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:84)   〜[hibernate-core-5.2.16.Final.jar:5.2.16.Final]在   pl.ultimo.web.odo.ODOBackend.incidents.entities.IncidentAssessment _ $$ _ jvst6f1_32.getIncidentAssessmentId(IncidentAssessment _ $$ _ jvst6f1_32.java)   〜[classes / :?]在   pl.ultimo.web.odo.ODOBackend.incidents.mapper.ViolationCriterionMapperImpl.incidentAssessmentToIncidentAssessmentDto(ViolationCriterionMapperImpl.java:150)   〜[classes / :?]在   pl.ultimo.web.odo.ODOBackend.incidents.mapper.ViolationCriterionMapperImpl.incidentAssessmentFactorToIncidentAssessmentFactorDto(ViolationCriterionMapperImpl.java:169)   〜[classes / :?]

0 个答案:

没有答案