Chan
Chan

Reputation: 2641

How to validate negative values with hibernate annotations?

I have a table that contains the stock. It's legacy database and the stock is kept in columns. The database has a constraint on the columns stating that they can't be negative values. But I only get the constrain violation after the Transaction is committed.

15:21:31,154  WARN JDBCExceptionReporter:77 - SQL Error: 2290, SQLState: 23000
15:21:31,154 ERROR JDBCExceptionReporter:78 - ORA-02290: check constraint (ERPDSS13.STKMAST_CON_QTY13) violated
ORA-06512: at "ERPDSS13.INLTRAN_UPD_STKMAST", line 25
ORA-04088: error during execution of trigger 'ERPDSS13.INLTRAN_UPD_STKMAST'

Is there a way with annotations you can specify that a column can't be negative. Below is the column mapping?

@Column(name = "STKSOHQTY01", precision = 12)
public BigDecimal getStksohqty01() {
    return this.stksohqty01;
} 

Upvotes: 0

Views: 2307

Answers (3)

Perception
Perception

Reputation: 80633

As of version 3.5 Hibernate supports standard bean validation (JSR303). You can and should use those to validate your entities as the API is fast, well-integrated, and more importantly, standardized:

@Min(value = 0)
@Column(name = "STKSOHQTY01", precision = 12)
public BigDecimal getStksohqty01() {
    return this.stksohqty01;
}

Upvotes: 1

Pramod Kumar
Pramod Kumar

Reputation: 8014

@Check(constraints = "STKSOHQTY01 >= 0")  
public class Coupon implements  Serializable {

}

It should work

Upvotes: 2

Jigar Joshi
Jigar Joshi

Reputation: 240948

Try with @Check

@Check(constraints = "STKSOHQTY01t >= 0")

public class YourEntity implements  Serializable {...}

Upvotes: 0

Related Questions