-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Demonstrate solution for lazy-loading #33
Comments
@glassfishrobot Commented |
@glassfishrobot Commented |
@glassfishrobot Commented Keep in mind, we have a very important design alternatives section to the site planned where we can discuss various design alternatives and their pros/cons so that folks know about them and can make their own choices. Also, we could make recommendations to the JPA EG that the EclipseLink behavior is more desirable and should be standardized . Thanks for logging the issue and your interest in the project! |
@glassfishrobot Commented For more complex domain models the extended entity manager looks promising. I put this very simple sample together: |
@glassfishrobot Commented First a few comments on the code for your consideration:
Did you get a chance to check with the DDD community as to their thoughts on lazy loading? Last I checked, the consensus appeared to be that lazy loading was largely antithetical to DDD practices as indicated on this thread as an anecdote: http://stackoverflow.com/questions/15851122/domain-driven-design-entity-lazy-loading. That being said, I would not go so far as to categorizing lazy loading into an anti-pattern. Clearly there are cases where it is a valid technique. I just want to know what the current thinking is (I am sure this issue had to have been discussed). Going the more classical DDD route, one option that might be of interest is JPA 2.1 entity graphs: http://www.datanucleus.org/products/datanucleus/jpa/entity_graphs.html. Another interesting observation is that lazy loading actually is dealt with somewhat gracefully using DTOs at the application layer. It's only in the simplified layering case that I was trying to demonstrate that appears more complexity need be introduced in the repository layer... Thoughts? |
@glassfishrobot Commented The nice thing with the extended persistance manager is that I can fetch the aggregate root object and traverse/modify the object graph in the the UI over several http request. Then when I am finished I call the save method on the reposity and all changes will be persisted/merged. Using the Word "flush" in the comment may have been a mistake. Your stackoverflow link has a valid Point, maybe I am mixing DDD and data access too much. I will have a look at entity graphs. |
@glassfishrobot Commented I am also aware of Adam's views on DDD and unfortunately differ from them. In my long years of enterprise development, I can't begin to cite examples when layering and API abstraction has proven to be invaluable in keeping systems flexible, maintainable and technology agnostic in the long run. That being said, we should document those views in this project as valid alternatives to choose, especially for relatively small, simple, young greenfield systems (sans the somewhat understandable misinterpretation/misunderstanding of the JPA automatic transaction enrollment/flush capability). |
@glassfishrobot Commented |
|
This could be done by |
Found a limitation when using The following definition does not work, https://stackoverflow.com/questions/17431312/what-is-the-difference-between-join-and-join-fetch-when-using-jpa-and-hibernate @NamedEntityGraph(
name = "Cargo.withItineraryLegs",
attributeNodes = {@NamedAttributeNode(value = "itinerary", subgraph = "itinerary")},
subgraphs = {
@NamedSubgraph(
name = "itinerary",
attributeNodes = {@NamedAttributeNode("legs")})
}) |
Use a join fetch in JPQL clause is working, see: aa99ee9#diff-b37a607550d90f061e56105444a10d95697c2e56e72b4ffe70e4af353ca143cfR51 |
Using join fetch is totally fine. It does not necessarily need to be entity graphs. |
I think this can be closed when develop branch is merged. |
There is some initial work on this done here: https://github.com/eclipse-ee4j/cargotracker/tree/wildfly-experimental. |
In the application some JPA relations are one-to-many and by default lazy loaded. When fetching the aggregate-root through the repository the returned root object will be detatched. Eclipselink handles this for read operations but Hibernate would throw a LazyInitializationException.
A traditional solution would be to add methods to a DAO such as loadCargoWithLoadedLegs();
With a lot of lazy collections and many combinations this would quickly be a bloated interface.
As a developer I would like to fetch the root, access and make changes to any object in graph, then store the root and have all changes persisted.
The text was updated successfully, but these errors were encountered: