Hy all,
I have three tables Child, Pet and Toy. Pet has a reference key the child id, and a toy has a reference key to a dog id.
I want to load all data about a Child and his pets, but i don’t want to load the toy data
@OneToMany(mappedBy = "petEntity", fetch = FetchType.LAZY)
public Set<PetEntity> getPetEntitySet() {
return petEntitySet;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ChildId", insertable = false, updatable = false)
public ChildEntity getChildEntity() {
return childEntity;
}
same for set of toys.
to load data i have
List<ChildEntity> list = entityManager.createQuery(
"SELECT c FROM ChildEntity c "+
"LEFT JOIN FETCH c.petEntitySet ",
ChildEntity.class).getResultList();
return list;
but this thing loads me all data, not just the information about child and his pets.
How can i suppress the entity manager to load only the data in the table, and not to make joins when i don’t want that
Thanks for your advices
i forgot to mention that in this chase it doesn’t only load all data, but it returns more than just one copy of a child elemnt
The reason why it is loading more data is because you are using the keyword
fetch. This willEAGERLYload whatever the child’s pets.Just remove the
fetchand lazy loading will occur.Update
I see that this is actually what you want, so just ensure
equals()andhashCode()is correctly overriden in all relevant classes, as I assume you are usingSet.In this way, jpa knows how to look for duplicates
Second update
Yes you can easily use
DISTINCTin your queries.Just add
DISTINCTin your select