Cosium / spring-data-jpa-entity-graph

Spring Data JPA extension allowing full dynamic usage of EntityGraph on repositories
MIT License
466 stars 51 forks source link

Setting the default EntityGraphType at the repository or entity level #44

Closed ddcruver closed 1 year ago

ddcruver commented 4 years ago

Is there a way to set the EntiyGraphType LOAD vs FETCH at the repository (not method) or at the entity level? For example I would like it to use the *.default entity graph but I would like to set the fetch type that it uses.

reda-alaoui commented 4 years ago

Previous versions of Hibernate were interpreting all EntityGraphs as javax.persistence.loadgraph. At that time, Hibernate was not respecting javax.persistence.fetchgraph type. Because of that, an attribute marked EAGER could never be turned back to LAZY by using an EntityGraph ignoring it. This is why I created the concept of default EntityGraph.

Thanks to https://hibernate.atlassian.net/browse/HHH-8776, and since 5.4.11, Hibernate now interprets correctly javax.persistence.fetchgraph type.

I don't know if other ORMs interpret correctly javax.persistence.fetchgraph type. If they do, I think default EntityGraph concept should be deprecated once #36 is fixed.

Are you using Hibernate?

ddcruver commented 4 years ago

Yes our product stack uses Hibernate. Most of our new development is built on Spring Boot.

ddcruver commented 4 years ago

I believe we are encountering the behavior where if our Entity Graph does not mention an attribute and regardless if that attribute is defined as EAGER it is LAZY it will include it so that would imply that it is treating our graph as a LOAD graph.

reda-alaoui commented 4 years ago

Are you still talking of *.default entity graphs?

ddcruver commented 4 years ago

I was talking about in general but yes that includes *.default.

reda-alaoui commented 4 years ago

Your original demand was:

Is there a way to set the EntiyGraphType LOAD vs FETCH at the repository (not method) or at the entity level? For example I would like it to use the *.default entity graph but I would like to set the fetch type that it uses.

Except *.default entity graph, do you have any other use case requiring the EntityGraphType to be passed at the repository or entity level instead of the method level?

ddcruver commented 4 years ago

Some of this may have already been said but just trying to get us both on the same thought path after being away from this issue for a few days...

The *.default entity graph from the Entity is used when none are provided at the repository method level, but there is no way to set the EntityGraphType that it uses that is use case 1, the original desire of this pull requests.

While thinking of it more there is a use case where you may want to set the EntityGraph at the repository class level with a corresponding EntityGraphType similar to how org.springframework.data.jpa.repository.EngityGraph does it on the repository method level. I will call this use case 2.

Use case 1 may be easier to implement and solves our personal use case. To extend on use case 1 if you need to add an annotation to set the desired EntityGraphType of an Entity maybe adding an argument to allow them to change the *.default so they could possibly name it whatever they like. @DefaultEntityGraph(name = "EntityGraphName", type = EntityGrarphType.LOAD) where name is an alias of the annotation value parameter.

reda-alaoui commented 1 year ago

Please read https://github.com/Cosium/spring-data-jpa-entity-graph/issues/60#issuecomment-1206602936

reda-alaoui commented 1 year ago

In the end, implemented in #75 . Please read https://github.com/Cosium/spring-data-jpa-entity-graph#repository-default-entitygraph for instructions.