Thoughts on Java

  • Blog
    • Hibernate & JPA Tutorials
    • Hibernate Tips
  • Videos
  • Book
  • Training
  • Work with me
  • About
    • About Thoughts on Java
    • Resources
  • Member Library
You are here: Home / Tips / Hibernate Tips: How to select multiple scalar values in a Criteria Query

Hibernate Tips: How to select multiple scalar values in a Criteria Query

August 12, 2016 By Thorben Janssen Leave a Comment

Hibernate Tips is a series of posts in which I describe a quick and easy solution for common Hibernate questions. If you have a question you like me to answer, please leave a comment below.

 

Question:

How can I select a list of scalar values in a Criteria query?
 

Solution:

The CriteriaQuery interface provides the multiselect() method which allows you to select multiple scalar values. The following code snippet shows an example for such a query.

The multiselect() method expects a List or an array of Selection interfaces which define the entity attributes which shall be fetched from the database. In this example, I use the JPA metamodel to reference the attributes in a type-safe way. When you execute such a CriteriaQuery, it returns a List of Tuple interface implementations. The Tuple interface provides convenient access to the selected values based on its position or its alias. In the code snippet, I defined an alias for each attribute in the query and use it to get them from the Tuple result.

Get this Hibernate Tip as a printable PDF!

Join the free Thoughts on Java Library to get access to lots of member-only content, like a printable PDF for this post, lots of cheat sheets and 2 ebooks about Hibernate.
Join Now!

Already a member? Login here.

Learn more:

You can not only use the Criteria API to create database queries, you can also write update and delete statements, as I show in this post: Criteria Update/Delete – The easy way to implement bulk operations with JPA2.1.

And if you want to use the Criteria API in your project, you should also have a look at the JPA metamodel. It provides a great way to create queries in a type-safe way. I explain it in detail in this post: Create type-safe queries with the JPA static metamodel.

 

Hibernate Tips Book


Get more recipes like this one in my new book Hibernate Tips: More than 70 solutions to common Hibernate problems.

It gives you more than 70 ready-to-use recipes for topics like basic and advanced mappings, logging, Java 8 support, caching and statically and dynamically defined queries.

Get it now as a paperback, ebook or PDF.

Filed Under: Tips

Implement Your Persistence Layer with Ease

Learn More About Hibernate

Need Some Help with Your Project?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Join over 5.000 developers
in the
Thoughts on Java Library

Ebooks Sidebar Get free access to ebooks, cheat sheets and training videos.
Join Now!
Special Launch Price

Let’s Connect


Thorben Janssen
  • Facebook
  • Github
  • Google+
  • Twitter
  • YouTube

Speaking at

8th March 2018
Voxxed Days Zurich (Talk):
Hibernate Tips ‘n’ Tricks - 15 Tips to solve common problems

13th March 2018
JavaLand (Talk):
Hibernate - State of the Union

15th March 2018
JavaLand (Workshop):
Hibernate Performance Tuning

10th-11th April 2018
Munich (Training):
Hibernate für Fortgeschrittene

12th-13th April 2018
Munich (Training):
Hibernate Performance Tuning

April 2018
JAX (Talk):
Hibernate Tipps und Tricks: Schnelle Lösungen für typische Probleme und Anwendungsfälle

April 2018
JAX (Workshop):
Hibernate-Workshop für Fortgeschrittene

Looking for an inhouse training?

Featured Post

Getting Started With Hibernate

Getting Started With Hibernate

12 Java YouTube Channels You Should Follow In 2018

12 Java YouTube Channels You Should Follow In 2018

Ultimate Guide – Association Mappings with JPA and Hibernate

Ultimate Guide – Association Mappings with JPA and Hibernate

Ultimate Guide to JPQL Queries with JPA and Hibernate

Ultimate Guide to JPQL Queries with JPA and Hibernate

Hibernate Best Practices

Hibernate Best Practices

Recent Posts

  • 5 Common Hibernate Mistakes That Cause Dozens of Unexpected Queries
  • Hibernate Tips: How to map an entity to multiple tables
  • Hibernate Tips: Easiest way to manage bi-directional associations
  • Hibernate & jOOQ – A Match Made in Heaven
  • Hibernate Tips: How to avoid Hibernate’s MultipleBagFetchException
  • Getting Started with jOOQ – Building SQL Queries in Java
  • Composition vs. Inheritance with JPA and Hibernate
  • JPA Tips: How to map a Duration attribute
  • Hibernate Tips: How to use an ORDER BY clause in a CriteriaQuery
  • What’s the difference between JPA, Hibernate and EclipseLink

Copyright © 2018 · Thoughts on Java

  • Impressum
  • Disclaimer
  • Privacy Policy
Thoughts on Java