2

I have a product class with attribute like this :

Boolean latest;

public boolean isLatest() {
    return latest;
}

public void setLatest(boolean latest) {
    this.latest = latest;
}

In database the attribute is bit type with true/false value.

I want to select products which have latest = true. My hql is:

FROM Product WHERE latest = true

I also tried:

FROM Product p WHERE p.isLatest is true

FROM Product WHERE latest is true

But it's always return all products or failed. Is there any way to select the products which have latest attribute = true. Any help would be great.

8
  • 1
    I think it depends on how the boolean is stored. See this question for more info: stackoverflow.com/q/1154833/461499 Commented Nov 19, 2014 at 15:30
  • Just wandering if it works if you add getLatest() instead of isLatest() to your entity? Commented Nov 19, 2014 at 15:32
  • @Predrag isLatest() is standard notation for Boolean. I'm not sure if it will be the problem, but you should not mix Boolean and boolean. Use Boolean if it can be null. If it cannot be null, use boolean. Commented Nov 19, 2014 at 15:49
  • 3
    This simply works (just tried on my project, is true and = true both work, with isLatest()), so it's got to be something small enough to be easily overlooked. Try changing the type from Boolean to boolean, since your getter and setter are using primitive boolean type. Commented Nov 19, 2014 at 15:49
  • Which database? @bradleyfitz: is is for boolean. Commented Nov 19, 2014 at 21:18

1 Answer 1

4

When I ran into this, I was using JDBC on an Apache Derby database. I was trying to set the value of a boolean field and was trying "=true", "=1", etc. In the end, I used a named parameter as shown below and that's how I got my situation to work.

session.createQuery("SELECT something FROM Product WHERE latest = :latest").setBoolean("latest", Boolean.TRUE);

Hopefully this helps someone with a similar situation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.