简体   繁体   中英

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!

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM