A select JPA query that ignores the related entities of one-to-many relation, is that possible?

StackOverflow https://stackoverflow.com/questions/11194164

Pregunta

I am new in JPA, so excuse me if my question seems basic. I have an entity called User, which is related to a list of other entities like follow:

@OneToMany(cascade = CascadeType.ALL , mappedBy = "user")
private List<session> sessionList;

In a controller class, I defined a find method in a RESTFull manner like follow:

@GET
@Path("/Users")
@Produces("application/json")
public List<UserDevice> findAllUsers()
{
    return em.createQuery("SELECT u FROM User u").getResultList();
}

The returned result contains all the sessions of the users which is normal, but make the result huge though I just want to retrieve the basic information of the users (all the simple columns). My question is: is it possible to ignore the related entities and just keep the columns of the actual entity? Thank you very much

¿Fue útil?

Solución

Unless you explicitely map the association as eagerly loaded (using @OneToMany(fetch = FetchType.EAGER)), the above query should only return the fields of the users, and should not load their sessionList.

If the sessions are loaded, then the association is marked as eagerly loaded, or you load them lazily by calling a method of the List<Session>.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top