glasspill
glasspill

Reputation: 1300

Where clause in Criteria API query

I'm just starting with the JPA Criteria API have a few questions:

What is the difference between the different Subinterfaces of Expression and when should they be used? In what different scenarios for say, a where clause?

When do you need the Metamodel of an Entity and when should you just access the attribute with entity.get("name")?

Any answers and decent tutorial links are appreciated.

Thank you

Upvotes: 1

Views: 3974

Answers (1)

perissf
perissf

Reputation: 16273

After reading the Java EE 6 Tutorial, I strongly suggest this article.

My experience in Metamodel is that it's the best way in order to minimize the risk of having run-time errors when running the queries, because Metamodel makes it possible to use the field names instead of the corresponding string values. There are more examples on this site. Some are:

JPA: Selecting entities based on multiple criterions on multiple child entities

How to use JPA Criteria API when joining many tables

JPA / Hibernate: CriteriaBuilder - How to create query using relationship object?

https://stackoverflow.com/a/3842319/870122

In the links above you will see real-life examples of the use of some sub-interfaces of Expressions: Predicates, Root, Join, from, etc... (here another useful link on the topic)

Upvotes: 2

Related Questions