Java – is there any way to prevent hibernate from destroying Boolean text in @ where annotation?

I want to use the @ where annotation in hibernate to delete objects that have been marked "deleted" by the Boolean attribute on the object For example, the following should prevent hibernate from loading any deleted addresses:

@OneToMany(mappedBy="contact")
@Where(clause="deleted=FALSE")
private Set<Address> addresses;

However, when I use a clause like deleted = false, hibernate will destroy the Boolean text by adding a table name before it, which will cause the query to fail For example:

select ... from address address0_ where  ( address0_.deleted=address0_.FALSE)  and address0_.contact_id=?

I expect (address0_. Deleted = false) instead of (address0_. Deleted = address0_. False)

Is there any way to specify the @ where clause or configure hibernate to output Boolean values correctly?

PS. note that some databases can specify Boolean values as string literals, as follows:

@Where(clause="deleted='FALSE'")

That will be converted to (address0_. Deleted = 'false'), which works well in, for example, PostgreSQL But I use HSQLDB in this project, and HSQLDB doesn't seem to support Boolean string literals On hsql, when deleted = 'false' is used, I get the following exception:

org. hsqldb. Hsqlexception: data exception: invalid character value cast

Solution

I found that the error report on this has not been solved for more than six years! For this problem, the hibernate problem tracker includes hhh-1587, hhh-2775 and ann-647

The solution is to create a custom dialog class, which registers true, false and unknow as keywords (these are the official Boolean text in the SQL specification) This causes hibernate to recognize them as keywords and stop prefixing them as if they were columns

This is my custom dialog class, which solves this problem for me:

public class ImprovedHsqlDialect extends HsqlDialect {

    public ImprovedHsqlDialect() {
        super();
        registerKeyword("true");
        registerKeyword("false");
        registerKeyword("unkNown");
    }
}

Once this dialect is used, @ where (clause = "deleted = false") will work normally

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>