user373201
user373201

Reputation: 11455

jpa criteriabuilder query disable eager fetching

I have a person object that has a list of addresses and in the mapping it is annotated to be fetched eagerly.

I have a criteriabuilder query where, say i want to fetch a person object but without fetching the address. how do i disable the eager feting in criteria query alone. is there a method?

The actual code is not this simple...this is just to give you an example.

Thanks in advance

Upvotes: 2

Views: 4323

Answers (2)

James
James

Reputation: 18389

It is normally better to use LAZY fetching in the mapping, and then fetch it eagerly when required using the JPQL "join fetch" option.

In JPA you could just select the data that you want from the entity, instead of the entire entity, then you could avoid the relationships. You could also use a constructor query to create shell instances (note these would not be managed).

A way to make an eager relationship lazy in a query is to use fetch groups. JPA does not support fetch groups, but some JPA providers such as EclipseLink do. In EclipseLink you can define a fetch group using the @FetchGroup annotation, or query hints.

Upvotes: 3

CMR
CMR

Reputation: 985

I think it is possible. From OpenJPA documentation:

You can specify a default subclass fetch mode for an individual class with the metadata extension described in Section 9.1.1, “ Subclass Fetch Mode ”. Note, however, that you cannot "upgrade" the runtime fetch mode with your class setting. If the runtime fetch mode is none, no eager subclass data fetching will take place, regardless of your metadata setting.
This applies to the eager fetch mode metadata extension as well (see Section 9.2.1, “ Eager Fetch Mode”). You can use this extension to disable eager fetching on a field or to declare that a collection would rather use joins than parallel selects or vice versa

Section 9.2.1 here.

Upvotes: 0

Related Questions