Hibernate OneToMany未填充列表

时间:2019-02-28 23:37:55

标签: java hibernate

我已经检查了所有类似问题的答案,但没有找到解决方案。我对OneToMany Hibernate应用程序有问题。我有两个非常简单的实体:

@Entity
public class SWBSubmission {

    private Integer swbSubmissionID;
    private Integer shipmentID;
    private String declarationNo;
    private List<SWBSubmissionAttempt> attempts = new ArrayList<>();

    @Id
    @GeneratedValue
    public Integer getSWBSubmissionID() {
        return swbSubmissionID;
    }

    public void setSWBSubmissionID(Integer swbSubmissionID) {
        this.swbSubmissionID = swbSubmissionID;
    }

    public Integer getShipmentID() {
        return shipmentID;
    }

    public void setShipmentID(Integer shipmentID) {
        this.shipmentID = shipmentID;
    }

    @Column(insertable = false, updatable = false)
    @Generated(GenerationTime.ALWAYS)
    public String getDeclarationNo() {
        return declarationNo;
    }

    public void setDeclarationNo(String declarationNo) {
        this.declarationNo = declarationNo;
    }

    @OneToMany(mappedBy="swbSubmission")
    public List<SWBSubmissionAttempt> getAttempts() {
        return attempts;
    }

    public void setAttempts(List<SWBSubmissionAttempt> attempts) {
        this.attempts = attempts;
    }
}

@Entity
public class SWBSubmissionAttempt {

    private Integer swbSubmissionAttemptID;
    private Timestamp submissionDateTime;
    @ManyToOne
    @JoinColumn(name="SWBSubmissionID", nullable=false)
    private SWBSubmission swbSubmission;


    @Id
    @GeneratedValue
    public Integer getSwbSubmissionAttemptID() {
        return swbSubmissionAttemptID;
    }

    public void setSwbSubmissionAttemptID(Integer swbSubmissionAttemptID) {
        this.swbSubmissionAttemptID = swbSubmissionAttemptID;
    }

    @ManyToOne
    @JoinColumn(name = "SWBSubmissionID")
    public SWBSubmission getSwbSubmission() {
        return swbSubmission;
    }

    public void setSwbSubmission(SWBSubmission swbSubmission) {
        this.swbSubmission = swbSubmission;
    }

    public Timestamp getSubmissionDateTime() {
        return submissionDateTime;
    }

    public void setSubmissionDateTime(Timestamp submissionDateTime) {
        this.submissionDateTime = submissionDateTime;
    }
}

两个表上的更新均正常运行。例如,这是同时插入两者的数据:

SWB提交:

SWBSubmissionID ShipmentID  DeclarationNo
1   15  2019FEB00000015
2   16  2019FEB00000016

和SWBSubmissionAttempt

SWBSubmissionAttemptID  SWBSubmissionID SubmissionDateTime
1   2   2019-02-28 10:50:19.687
2   2   2019-02-28 11:16:50.173
3   2   2019-02-28 11:17:37.973
5   2   2019-02-28 13:49:40.957

这是我的问题。在我的应用程序中,执行以下操作时,我没有从详细信息表中获取记录:

SWBSubmissionService swbSubmissionService = new SWBSubmissionServiceImp();
shipmentService.beginTransaction();
Integer increment = swbSubmissionService.findByShipmentID(shipment.getShipmentID()).getAttempts().size() + 1;
System.println(increment)
shipmentService.commitTransaction();

由于SWBSubmission找不到SWBSubmissionAttempt的记录,因此增量始终为零。

我尝试了fetch.EAGER;尝试刷新,但这没什么区别。否,报告了错误消息,并且在cfg文件中打开了SHOW_SQL,我没有看到为明细表生成任何SQL。我整天都在这里。

任何人都知道。

谢谢

克莱德

0 个答案:

没有答案