How to write JPA query with boolean condition

Rado Skrib picture Rado Skrib · Jun 6, 2011 · Viewed 64.3k times · Source

In my project i am using JPA 2.0 with eclipselink inplementation, an I have following problem:

I have defined entity with boolean column:

@Entity
public User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="USR_ID")
    private Short id;

    @Column(name="USR_NAME")
    private String name;

    @Column(name="USR_ACTIVE")
    private boolean active;

    .......

}

I want to create query which will return all active users, something like this:

select u from User u where u.active = TRUE;

But if I use that query I got exception that boolean can't be cast to Short (column in database is stored as smallint). Is there any correct way how to write this query?

Thanks

Answer

YUIOP QWERT picture YUIOP QWERT · Dec 7, 2015

Use the following form:

SELECT e FROM Employee e WHERE e.active = TRUE

See the docs for more info: