Using hibernate with Mysql and for testing I am using HSQL. There are many collections where we are using a @Where clause based filtering. For example:
@Column(name="CONDITIONS")
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="CATEGORIES_PKEY")
@Cascade({org.hibernate.annotations.CascadeType.DELETE})
@Where(clause="deleted_flag = 'false'")
public List<CategoryConditionMapping> getMappedConditions() {
return mappedConditions;
}
In MySQL ,
this works:
@Where(clause="deleted_flag = 'false'")
this doesn't work:
@Where(clause="deleted_flag = FALSE")
In HSQL ,
this works:
@Where(clause="deleted_flag = FALSE")
this doesn't work:
@Where(clause="deleted_flag = 'false'")
Hsql started supporting @Where(clause="deleted_flag = FALSE"), only after adding custom HSQLDialect
:
public class CustomHSQLDialect extends HSQLDialect {
public CustomHSQLDialect() {
super();
registerKeyword("true");
registerKeyword("false");
registerKeyword("unknown");
}
}
Tried registering for more keywords to this dialect., like
registerKeyword("'false'");
registerKeyword("'true'");
But it doesn't have any effect. Exception thrown by HSQL, while trying using @Where(clause="deleted_flag = 'false'")
Caused by: org.hsqldb.HsqlException: data exception: invalid character value for cast at org.hsqldb.error.Error.error(Unknown Source) at org.hsqldb.error.Error.error(Unknown Source) at org.hsqldb.Scanner.convertToBit(Unknown Source) at org.hsqldb.types.BitType.castOrConvertToType(Unknown Source) at org.hsqldb.types.BitType.castToType(Unknown Source) at org.hsqldb.ExpressionOp.getCastExpression(Unknown Source) at org.hsqldb.ExpressionLogical.resolveTypesForComparison(Unknown Source) at org.hsqldb.ExpressionLogical.resolveTypes(Unknown Source) at org.hsqldb.ExpressionLogical.resolveTypes(Unknown Source) at org.hsqldb.QuerySpecification.resolveExpressionTypes(Unknown Source) at org.hsqldb.QuerySpecification.resolveTypesPartOne(Unknown Source) at org.hsqldb.QueryExpression.resolve(Unknown Source) at org.hsqldb.ParserDQL.compileCursorSpecification(Unknown Source) at org.hsqldb.ParserCommand.compilePart(Unknown Source) at org.hsqldb.ParserCommand.compileStatement(Unknown Source) at org.hsqld b.Session.compileStatement(Unknown Source) at org.hsqldb.StatementManager.compile(Unknown Source) at org.hsqldb.Session.execute(Unknown Source)
HSQL is trying to convert string to boolean and fails.
How can we make HSQL understand string 'false' as a bit or how can we make @Where clause generic to work happily on both HSQL and MySQL
Maybe you are not using the right type for the column. The best way to use boolean values in MySQL is to use BIT or TINYINT and in the Entity class you define it like 'boolean'. If it doesn't work you should even add @Type like this:
@Column(name="DELETED_FLAG")
@Type(type = "org.hibernate.type.NumericBooleanType")
public boolean deleted_flag = true;
Then hibernate will know what to do with this property. Check this article for mysql data types: http://dev.mysql.com/doc/connector-j/en/connector-j-reference-type-conversions.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.