JPA Query.Getresultlist() - Use in a Generic Way

JPA Query.getResultList() - use in a generic way

General rule is the following:

  • If select contains single expression and it's an entity, then result is that entity
  • If select contains single expression and it's a primitive, then result is that primitive
  • If select contains multiple expressions, then result is Object[] containing the corresponding primitives/entities

So, in your case list is a List<Object[]>.

JPA named Query getResultList() in generic way

I don't know it's a valid solution or not but,
I resolve this by removing resultClass argument from createNamedQuery.

resultClass - the type of the query result

Also, it will throw IllegalArgumentException if mismatch the type.
From doc:

IllegalArgumentException - if a query has not been defined with the
given name or if the query string is found to be invalid or if the
query result is found to not be assignable to the specified type

So, I changed my method to this. And it works fine for now.

@Nonnull
public List<ProjectLevel<?>> findUUIDByNameorNumber(@Nonnull final String nameOrId,
@Nonnull final String businessAccountId) {
checkNotNull(nameOrId, "The nameOrId must not be null");

return getEntityManager()
.createNamedQuery("ProjectLevel.findUUIDByNameOrId")
.setParameter("name", matchesFromBeginning(nameOrId))
.setParameter("businessAccountId", businessAccountId)
.getResultList();
}

JPA query.getResultList()?

Your query selects many fields. Such a query always returns a list of Object arrays. If you want a list containing instances of your GmaThresholdParameter entity, then the query should be

select gmaTh from GmaThresholdParameter gmaTh 
where gmaTh.id.circleId=?1 AND gmaTh.id.tspId=?2 AND gmaTh.id.flag=?3

The code to get the list of entities would then be

List<GmaThresholdParameter> resultList = query.getResultList();

You'll get a type safety warning from the compiler, that you can ignore.

Casting the output of Query.getResultList()

You return individual object fields ...and get a number and a string.

Query query= em.createQuery("select id,name from SheetItem");

Return the object of the required type...

Query query= em.createQuery("select sh from SheetItem sh");

...and get the correct JSON.

Java Persistence: Cast to something the result of Query.getResultList()?

As a newcomer to JPA was taking this as the definitive answer but then I found a better one via this question: Why in JPA EntityManager queries throw NoResultException but find does not?

Its as simple as as using TypedQuery instead of Query e.g.:

TypedQuery<Person> lQuery = myEntityManager.createQuery("from Person", Person.class);
List<Person> personList = lQuery.getResultList();

How do we fetch the ResultSet in the desired format when the fields to be fetched are coming as part of the request in spring data jpa?

Here's how I've done it. Apparently you can get the ResultSet as a list of Tuple. I just had to iterate over this list and make a Map for each entry in result set so I can map it using ModelMapper. Here tags are a list of columns to be read.

    Query query = entityManager.createNativeQuery(preparedQuery,Tuple.class);
List<Tuple> resultList = query.getResultList();
List<Product> resultDto = new ArrayList<>();
for (Tuple tuple : resultList) {
HashMap<String, Object> data = new HashMap<>();
for (String tag : tags) {
data.put(tag, tuple.get(tag));
}
resultDto.add(mapper.map(data, Product.class));
}
return resultDto;

Thanks for all who responded to this.

javax.persistence.Query with Generics

You have to declare the generic type before you use it in List:

public <T extends UtilsStatus> List<T> allStatus(Class<T> type)



Related Topics



Leave a reply



Submit