Luigi Vorraro Luigi Vorraro - 5 months ago 73
MySQL Question

HIbernate Envers @AuditMappedBy cause SQLException: Parameter index out of range

in my project I use hibernate Envers (v. 4.3.7) with Spring and MySql (v. 5.6.25) for auditing some entities.

This is my example:

@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;
}

......
}


When try to insert a new Ticket the the system return the exception:

org.hibernate.exception.GenericJDBCException: could not insert:
.....
Caused by: java.sql.SQLException: Parameter index out of range (19 > number of parameters, which is 18)


Some idea?

Answer

I resolved. Into Payment class I modified the reference to Ticket:

Old code:

@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;
} 

New code:

    @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;
    }

I removed the "insertable = false, updatable = false" from getTktTicket() and added to getTicketId(). Maybe this is a bug of Envers.

Comments