HIbernate Envers @AuditMappedBy导致SQLException:参数索引超出范围

时间:2016-05-30 13:46:36

标签: java mysql spring hibernate hibernate-envers

在我的项目中,我使用带有Spring和MySql(v.5.6.25)的hibernate Envers(v.4.3.7)来审核某些实体。

这是我的例子:

@Audited
@Entity
@Table(name = "ticket")
public class Ticket implements java.io.Serializable {

    ....

    private List<Payment> payments = new ArrayList<>(0);

    ....

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "ticket")
    @AuditMappedBy(mappedBy = "ticket")
    public List<Payment> getPayments() {
        return this.payments;
    }

    public void setPayments(List<Payment> payments) {
       this.payments= payments;
    }
    ...
}


@Audited
@Entity
@Table(name = "payment")
public class Payment implements java.io.Serializable {

    ....

    private Ticket ticket;
    private Long ticketId;


    ....

    @Column(name = "TICKET_ID", nullable = false)
    public Long getTicketId() {
         return ticketId;
    }

    public void setTicketId(Long ticketId) {
         this.ticketId = ticketId;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "TICKET_ID", nullable = false, insertable = false, updatable = false)
    public Ticket getTicket() {
        return this.ticket;
    }

    public void setTicket(TktTicket ticket) {
        this.ticket= ticket;
    } 

   ......
}

当尝试插入新的Ticket时,系统会返回异常:

org.hibernate.exception.GenericJDBCException:无法插入: ..... 引起:java.sql.SQLException:参数索引超出范围(19>参数个数,即18)

有些想法?

1 个答案:

答案 0 :(得分:0)

我解决了。进入Pay​​ment类我修改了对Ticket的引用:

旧代码:

@Column(name = "TICKET_ID", nullable = false)
public Long getTicketId() {
     return ticketId;
}

public void setTicketId(Long ticketId) {
     this.ticketId = ticketId;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "TICKET_ID", nullable = false, insertable = false, updatable = false)
public Ticket getTicket() {
    return this.ticket;
}

public void setTicket(TktTicket ticket) {
    this.ticket= ticket;
} 

新代码:

    @Column(name = "TKT_TICKET_ID", nullable = false, insertable = false, updatable = false)
    public Long getTicketId() {
        return ticketId;
    }

    public void setTicketId(Long ticketId) {
        this.ticketId = ticketId;
    }
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "TKT_TICKET_ID", nullable = false)
    public TktTicket getTktTicket() {
        return this.tktTicket;
    }

    public void setTktTicket(TktTicket tktTicket) {
        this.tktTicket = tktTicket;
    }

我从getTktTicket()中删除了“insertable = false,updatable = false”并添加到getTicketId()。也许这是Envers的错误。