I've read Spring Data JPARepository: How to conditionally fetch children entites. But I want to use convenient JPA annotations rather than manual join all children.
Say I have the following Model:
@Entity
public class UserModel extends BaseModel<User> {
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<CredentialModel> credentialList = new ArrayList<>();
@ManyToMany
@JoinTable(
name = "users_actions",
joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "action_id", referencedColumnName = "id")
)
private List<ActionMode> actionList = new ArrayList<>();
}
Fetching credentialList
and actionList
can be a time consuming operation (join fetches, etc). I do not want to auto fetch credentialList
nor actionList
. But when I access them, I expect them to be an empty List rather than LazyInitializationException
.
Can I use the fields even when I did not specifically JOIN FETCH
them in @Query
. Just leave it to be an empty list.
If not, Is there anyway to achieve same needs?