我在这些对象上的实体映射存在一些问题。我没有例外,但似乎进入了递归循环

public class LabResult  implements java.io.Serializable {

     private Long labResultId;
     private Customer customer;
     private LabResultUnprocessed labResultUnprocessed;

public LabResult(){
}

public LabResult(Long labResultId) {
    this.labResultId = labResultId;
}

public LabResult(Long labResultId, Customer customer, LabResultUnprocessed labResultUnprocessed) {
    this.labResultId = labResultId;
    this.customer = customer;
    this.labResultUnprocessed = labResultUnprocessed;
}

@OneToOne(fetch=FetchType.LAZY, mappedBy="labResult")
@JoinColumn(name="lab_result_id")
public LabResultUnprocessed getLabResultUnprocessed(){
    return labResultUnprocessed;
}
public void setLabResultUnprocessed(LabResultUnprocessed labResultUnprocessed) {
    this.labResultUnprocessed = labResultUnprocessed;
}


下一个域是LabResultUnprocessed

@Entity
@Table(name="lab_result_unprocessed"
,schema="public"
)
public class LabResultUnprocessed implements java.io.Serializable {

private LabResult labResult;
private Boolean processedFlag;

public LabResultUnprocessed() {

}

public LabResultUnprocessed(LabResult labResult, Boolean processedFlag) {
    this.labResult = labResult;
    this.processedFlag = processedFlag;
}

@Id
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="lab_result_id")
public LabResult getLabResult() {
    return labResult;
}

public void setLabResult(LabResult labResult) {
    this.labResult = labResult;
}


这是LabResultUnprocessedRepository

public interface LabResultUnprocessedRepository extends CrudRepository<LabResult, String>{

@Query("select lru from LabResultUnprocessed lru "
        +" join fetch lru.labResult lr "
        +" where lru.labResult.labResultId = lr.labResultId "
        +" and lru.processedFlag = false")
List<LabResultUnprocessed> findAllByProcessedFlag();


在我的服务中,当我调用此方法时,它似乎进入了递归循环,并且从未碰到我在第二行中实际方法调用上的断点。

    List<LabResultUnprocessed> allUnprocessedResults = new ArrayList<LabResultUnprocessed>();
    allUnprocessedResults = labResultUnprocessedRepository.findAllByProcessedFlag();

    allUnprocessedResults.forEach(lru -> {
    ...////

最佳答案

本节中有两个问题:

@OneToOne(fetch=FetchType.LAZY, mappedBy="testResult")
@JoinColumn(name="test_result_id")



mappedBy@JoinColumn不能一起使用。关系的一端应该有一个,另一端应该有另一个。任何一端都不应同时具有两者。为此删除@JoinColumn来解决此问题。
mappedBy的值必须是关系另一端的字段名称-在本例中为labResult

10-06 05:50