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
@Danny I have updated the answer that will answer your question. The SQL query is in written in JPQL. You can also use native SQL query if you want with NativeQuery option. lr is an alias to LoanReport entity to be used in other parts of a JPQL query.
We can use @Query annotation to specify a query within a repository. Following is an example. In this example, we are using native query, and set an attribute nativeQuery=true in Query annotation to mark the query as native. We've added custom methods in Repository in JPA Custom Query chapter.
Its findById method retrieves an entity by its id. The return value is Optional<T> . Optional<T> is a container object which may or may not contain a non-null value. If a value is present, isPresent returns true and get returns the value.
Use the following form:
SELECT e FROM Employee e WHERE e.active = TRUE
See the docs for more info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With