Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

TypedQuery rejects a query that selects several columns

Errorjava.lang.IllegalArgumentException

Short answer

The Jakarta Persistence rule is that the select list of a typed query holds a single item. When you select two or more columns and pass an entity class as the result type, the provider cannot fit the row into it. Ask for Object[] or Tuple, or give a class whose constructor matches the select list.

  • Applies to Hibernate ORM 6.6, Jakarta Persistence 3.2, Java 21, PostgreSQL 16
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Look at the select clause of the failing query and at the class you passed as the second argument to createQuery. Count the items on the left and compare them with that class.

    grep -rn "createQuery(" src/main/java
  2. 2Match what you see, then jump to its fix

    1. 1select u.id, u.name from User u, with User.class as the result typeSeveral columns selected, but an entity class passed as the result typeFix
    2. 2Object[] works but the index based code is hard to readYou want named access to each column instead of positionsFix
    3. 3You keep copying Object[] values into a class by handYou want a typed result object (a DTO)Fix
    4. 4IllegalArgumentException saying the result is not assignable to the specified typeThe result type is wrong even for a single itemFix

What does the error look like?

  • java.lang.IllegalArgumentException
  • The select list of the query must contain only a single item
  • IllegalArgumentException: query result is found to not be assignable to the specified type

Cause 1 of 4

Several columns selected, but an entity class passed as the result type

1Check

The result type tells the provider what one row becomes. An entity class only fits a select list that returns that entity.

// fails
em.createQuery("select u.id, u.name from User u", User.class)

2Fix

Ask for Object[] and read each position by index.

List<Object[]> rows = em.createQuery(
        "select u.id, u.name from User u", Object[].class)
    .getResultList();
for (Object[] row : rows) {
    Long id = (Long) row[0];
    String name = (String) row[1];
}

3Verify

Run the query once; it returns rows instead of throwing.

./mvnw -q test

Confirmed by docs.hibernate.org

Did this fix it?

Cause 2 of 4

You want named access to each column instead of positions

1Check

Give each select item an alias and ask for jakarta.persistence.Tuple.

em.createQuery("select u.id as id, u.name as name from User u", Tuple.class)

2Fix

Read values with tuple.get(alias, type).

List<Tuple> rows = em.createQuery(
        "select u.id as id, u.name as name from User u", Tuple.class)
    .getResultList();
for (Tuple t : rows) {
    Long id = t.get("id", Long.class);
    String name = t.get("name", String.class);
}

3Verify

The loop runs without a cast error.

./mvnw -q test

Confirmed by docs.hibernate.org

Did this fix it?

Cause 3 of 4

You want a typed result object (a DTO)

1Check

Create a record or class whose constructor parameters match the select list exactly, in the same order. Hibernate 6 accepts such a class as the result type; the standard JPA form is select new.

record UserSummary(Long id, String name) {}

2Fix

Pass the record as the result type, or use the select new form with the class name.

List<UserSummary> a = em.createQuery(
        "select u.id, u.name from User u", UserSummary.class)
    .getResultList();

List<UserSummary> b = em.createQuery(
        "select new UserSummary(u.id, u.name) from User u", UserSummary.class)
    .getResultList();

3Verify

Both queries return UserSummary objects.

./mvnw -q test

Confirmed by docs.hibernate.org

Did this fix it?

Cause 4 of 4

The result type is wrong even for a single item

1Check

The specification throws IllegalArgumentException when the query result cannot be assigned to the type you named, for example selecting u.name and asking for Long.class.

grep -rn "createQuery(" src/main/java

2Fix

Make the second argument match what the select list returns, such as String.class for a single name.

List<String> names = em.createQuery(
        "select u.name from User u", String.class)
    .getResultList();

3Verify

The query returns a List<String>.

./mvnw -q test

Confirmed by jakarta.ee

Did this fix it?

How do I stop it coming back?

  • Pick the result type first: entity for whole rows, a record for a projection, Object[] only for quick one-offs.
  • Alias every select item so a later change in column order cannot break your code.
  • Put each projection query in a test that runs against the real database type.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Databases

Java cannot load org.postgresql.Driver

Class not found loading JDBC org.postgresql.Driver

The PostgreSQL JDBC jar is not on the classpath of the process that runs your code.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.