TypedQuery rejects a query that selects several columns
java.lang.IllegalArgumentExceptionShort 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.
Fix it fast
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/java2Match what you see, then jump to its fix
- 1
select 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
Object[] works but the index based code is hard to readYou want named access to each column instead of positionsFix - 3
You keep copying Object[] values into a class by handYou want a typed result object (a DTO)Fix - 4
IllegalArgumentException saying the result is not assignable to the specified typeThe result type is wrong even for a single itemFix
- 1
What does the error look like?
java.lang.IllegalArgumentExceptionThe select list of the query must contain only a single itemIllegalArgumentException: 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 testConfirmed by docs.hibernate.org
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 testConfirmed by docs.hibernate.org
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 testConfirmed by docs.hibernate.org
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/java2Fix
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 testConfirmed by jakarta.ee
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
H2 in PostgreSQL mode still behaves differently from PostgreSQL
Names or null ordering differ from PostgreSQL in test results
H2 compatibility modes are switched on in the JDBC URL and cover only a small part of the differences between databases.
4 causes, each with a fixDatabases
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 fixDatabases
PostgreSQL JDBC: an I/O error occurred while sending to the backend
org.postgresql.util.PSQLException: An I/O error occurred while sending to the backend.
This error means the connection the driver was about to use was already closed.
3 causes, each with a fixDatabases
executeQuery on an INSERT or UPDATE throws in PostgreSQL JDBC
No results returned by the query
executeQuery is for statements that return rows.
2 causes, each with a fixDatabases
Saving a UUID through JDBC fails with a type mismatch
Hint: You will need to rewrite or cast the expression.
pgJDBC sends setString parameters as varchar, and PostgreSQL will not store varchar in a uuid column without a conversion.
3 causes, each with a fixDatabases
Hibernate insert fails: column is of type bit but expression is of type boolean
ERROR: column "active" is of type bit but expression is of type boolean
PostgreSQL has a real boolean type and a separate bit type, and it will not convert one to the other on insert.
3 causes, each with a fix