Many to many relation with Spring Data Rest

Viewed 36

I have a question regarding Many to Many relation and how is it handled by Hibernate.

I have to entities I need to be connected. I decided to use unidirectional Many to Many, so I defined it like:

    @ManyToMany
    @JoinTable(name = "x_y_RELATION",
            joinColumns = @JoinColumn(name = "x_id"),
            inverseJoinColumns =
                    @JoinColumn(name = "y_id", updatable = false, insertable = false)
    )
    private Set<YEntity> yEntities;

To get the x entities I'm using @RepositoryRestResource(collectionResourceRel = "x-api", path = "x-api") on interface extending CrudRepository<X, Long>.

My problem is that during findAll() method (which I specified as native query for irrelevant reasons, it's like SELECT * FROM X) query is executed as defined, but then come messages from Hibernate logger that it performs next selects to fetch the Y. My assumption was that with default fetch type = LAZY this data won't be instantly fetched. Am I missing something? Or this is just default behavior in such a case? Can I modify it? I know I can create second entity for X without this relation, but I'm trying to find other solution.

EDIT

To have better understanding of what's going on I added test endpoint to omit @RepositoryRestResource and it looks like everything on the db/hibernate level is ok. I was able to execute findAll() and return x entities without fetching anything from ys. Looks like Spring Data Rest enforces response to be fully loaded. Is there a way to configure it?

0 Answers
Related