user1654894
user1654894

Reputation: 11

JPA / Get list of entities as references

The EntityManager provides a method getReference().

Is there something similar in JPQL or event better within CriteriaBuilder, that would return a list of entities just as references?

Actual I use just the ID and process each entity in a new transaction. I want to avoid the get all entities, because it would mess up my memory usage.

If I could use references, I would be type safe and the memory overhead would be okay.

Thanks!

Upvotes: 1

Views: 2143

Answers (2)

Hesam Talebi
Hesam Talebi

Reputation: 1

to ensure type-safety in criteria case, you can do this:

CriteriaQuery<Long> criteria =criteriaBuilder.createQuery(entityClass);
Root<T> r=criteria.from(entityClass);

//Where clauses

criteria.select(r.<Long>get("id"));

TypedQuery<Long> query=entityManager.createQuery(criteria);
List<Long> result=query.getResultList();

Upvotes: 0

John Ament
John Ament

Reputation: 11723

You could also do something like

select idField from YourEntity where ..

and similarly in your criteria query, just use the YourEntity.idField_ value.

Upvotes: 1

Related Questions