Anh Nguyen
Anh Nguyen

Reputation: 313

Use boolean value in HQL

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.

Upvotes: 2

Views: 12182

Answers (1)

GiantLeprechaun
GiantLeprechaun

Reputation: 134

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.

Upvotes: 4

Related Questions