简体   繁体   中英

How to pass boolean parameter to HQL new constructor

Situation : I have an object SubjectRow that wraps object Subject (object Subject is persistable entity). I need to create new wrapper object directly in HQL.

public class SubjectRow{
    Subject subject;
    String myString;
    boolean myBoolean;

    public SubjectRow(SubjectSch subject, String myString) {
        this.subject = subject;
        this.myString = myString;
    }

    public SubjectRow(SubjectSch subject, boolean myBoolean) {
        this.subject = subject;
        this.myBoolean = myBoolean;
    }


}

HQL for constructor with string (and it works like it should):

SELECT new package.SubjectRow(s, 'myString') FROM Subject s

Problem . The problem that sometimes I need to call another constructor of this wrapper that accepts boolean instead of string. I tried the same approach

SELECT new package.SubjectRow(s, true) FROM Subject s

But Hibernate complains that it can't find appropriate constructor in class SubjectRow. Of course, I could pass string and then just make appropriate casting - but its too ugly.

Question : how should I change my HQL and/or constructor to pass this boolean parameter directly (it is possible to use Boolean instead)?

"Solution" . Because I was unable to find how to pass boolean correctly I have to rework my domain objects little bit. I just remove this boolean property from SubjectRow and instead create SubjectRowFalse and SubjectRowTrue classes that are children of original object. And now just using two HQL

SELECT new package.SubjectRowTrue(s) FROM Subject s

SELECT new package.SubjectRowFalse(s) FROM Subject s

Of course, it is just a workaround and not solution for the original problem

我不是百分百肯定,但我认为您可能需要使用布尔对象而不是布尔基元。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM