使用Hibernate

时间:2016-06-29 12:59:23

标签: java mysql hibernate

主题标题可能有点误导,但我的问题是:

我有这三个表,使用Hibernate(带注释)进行映射:

@SuppressWarnings("serial")
@Entity
@Table(name = "payment", catalog = "db")
public class Payment implements java.io.Serializable {

    private Long paymentId;
    private Set<PaymentStateHistory> paymentStateHistory = new HashSet<>(0);

    @Id
    @Column(name = "payment_id", unique = true, nullable = false)
    public long getPaymentId() {
        return this.idSolicitud;
    }

    public void setPaymentId(Long paymentId) {
        this.paymentId = paymentId;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "payment")
    public Set<PaymentStateHistory> getPaymentStateHistory() {
        return this.paymentStateHistory;
    }

    public void setPaymentStateHistory(Set<PaymentStateHistory> paymentStateHistory) {
        this.paymentStateHistory = paymentStateHistory;
    }
}
@SuppressWarnings("serial")
@Entity
@Table(name = "payment_state_history", catalog = "db")
public class PaymentStateHistory implements java.io.Serializable {

    private Long stateHistoryId;
    private PaymentState paymentState;
    private Payment payment;
    private Date stateDate;

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "state_history_id", unique = true, nullable = false)
    public Long getStateHistoryId() {
       return this.stateHistoryId;
    }

    public void setStateHistoryId(Long stateHistoryId) {
       this.stateHistoryId = stateHistoryId;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "state_id", nullable = false)
    public PaymentState getPaymentState() {
       return this.paymentState;
    }

    public void setPaymentState(PaymentState paymentState) {
       this.paymentState = paymentState;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "payment_id", nullable = false)
    public Payment getPayment() {
       return this.payment;
    }

    public void setPayment(Payment payment) {
       this.payment = payment;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "state_date", length = 19)
    public Date getStateDate() {
       return this.stateDate;
    }

    public void setStateDate(Date stateDate) {
       this.stateDate = stateDate;
    }
}
@SuppressWarnings("serial")
@Entity
@Table(name = "payment_state", catalog = "db")
public class PaymentState implements java.io.Serializable {

    private Integer paymentStateId;
    private String info;
    private Set<PaymentStateHistory> paymentStateHistory = new HashSet<>(0);

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "payment_state_id", unique = true, nullable = false)
    public Integer getPaymentStateId() {
        return this.paymentStateId;
    }

    public void setPaymentStateId(Integer paymentStateId) {
        this.paymentStateId = paymentStateId;
    }

    @Column(name = "info", nullable = false, length = 100)
    public String getInfo() {
        return this.info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "paymentState")
    public Set<PaymentStateHistory> getPaymentStateHistory() {
        return this.paymentStateHistory;
    }

    public void setPaymentStateHistory(Set<PaymentStateHistory> paymentStateHistory) {
        this.paymentStateHistory = paymentStateHistory;
    }
}

基本上,payment_state_history会维护付款状态更改的历史记录,每个状态都会更改状态。

这种方式的映射工作正常,付款包含一系列所有状态的变化,但我想知道是否有一种方法可以映射这些关系,以便我可以保存最后(最新日期)付款状态中的PaymentState,因此我只需加载所需付款即可访问,并执行payment.getLastState()之类的操作。

付款表还有一个&#34; last_state_id&#34;列,在payment_state_history表中将该行与该付款的最后一个状态相关联(每次状态更改时它都会更新)。我知道我可以只映射那个id,并使用该id来查找相应的State,但它会向数据库重新查询更多查询,从而使系统变慢,因此在Payment上保存PaymentState对象将是理想的。

提前致谢。

1 个答案:

答案 0 :(得分:1)

您可以在付款实体

中添加以下新Memeber
private PaymentStateHistory lastPaymentStateHistory;

添加getter和setter,如下所示

public void setLastPaymentStateHistory(){
       //code to loop throught paymentStateHistory and set the lastPaymentStateHistory
}
@Transient
public void getLastPaymentStateHistory(){ 
   return this.lastPaymentHistory;
}

然后更新paymentStateHistory的setter,如下所示

    public void setPaymentStateHistory(Set<PaymentStateHistory> paymentStateHistory) {
    this.paymentStateHistory = paymentStateHistory;
    setLastPaymentStateHistory();
}