Dmitry Senkovich
Dmitry Senkovich

Reputation: 5911

How do I project a single column in JPA Criteria API?

I need to query a table and project only 1 column. In Hibernate Criteria API there is such thing as Projections class. But the Hibernate Criteria API is deprecated now and I need to switch to using JPA Criteria API. I know about the following opportunity:

cb.construct(EmpMenu.class,
      c.get("name"), c.get("department").get("name"));

But the problem is that I need to retrieve 1 property only. In this approach above I would create a dummy object wrapper just for sake of mapping. I would define the needed field there and it will work.

But is there a clearer way to do it? Thank you!

Upvotes: 0

Views: 571

Answers (1)

user3973283
user3973283

Reputation:

There are many JPA Criteria docs on the internet to explain that. For example this one. In simple terms you select the path of the field, like this

Path nameField = candidateRoot.get(Person_.name);
crit.select(nameField);

Upvotes: 1

Related Questions