Is it possible to set the hibernate to save -1 instead of 1 as true value for a Boolean field in the database? I need -1 to maintain compatibility with other program made in Delphi.
@Type(type="com.sample.type.CustomClass")
@Column(name = "TEST_FLAG")
private boolean testFlag;
The @Type
annotation needs a full path to the class that implements the userType interface; this is the factory for producing the target type of the mapped column
CustomClas.java which implements UserType interface provided by hibernate
UserType implementation provides developer to hook the custom logic/implementation and acts as a adapter between relational database and your class property.
You need to write the conversion logic in following methods.
nullSafeGet()
: Creates the custom object from the data returned by the result setnullSafeSet()
: Converts custom object into value which needs to be passed to prepared statementCheck the API for detail on the hibernate site.
Why not use Integer instead of Boolean?
Integer value;
public void setValue(Boolean b) {
this.value = (b != null ? (b ? -1 : 0) : null);
}
public Boolean getValue() {
return (this.value != null ? (this.value == -1 ? true : false) : null);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With