persistence icon indicating copy to clipboard operation
persistence copied to clipboard

static constructor methods for entity graph nodes

Open gavinking opened this issue 1 month ago • 0 comments

One of these days (almost certainly not for JPA 4.0!) we should look at adding a way to construct EntityGraphs that resembles the new API for result set mappings, and the APIs for restrictions, etc, that we have been designing in the Data group. There's no good reason to need to go to the EntityManager to create a graph. (Hibernate already lets you instantiate one via a static method.)

So we could let you write something like:

import static jakarta.peristence.EntityGraph.*;

...

graph(Book.class, 
    node(Book_.publisher,
        graph(Publisher.class
            node(Publisher_.whatever)
        )
    ),
    node(Book_authors)
)

or:

import jakarta.peristence.*;

...

EntityGraph.of(Book.class, 
    Node.of(Book_.publisher,
        Subgraph.of(Publisher.class
            Node.of(Publisher_.whatever)
        )
    ),
    Node.of(Book_authors)
)

Or whatever. I have not yet put any thought into what it would really look like, it's just an idea that's been in the back of my head for quite a long time now.

This would just be a matter of adding maybe three record classes implementing those interfaces and some static methods to instantiate them. Perhaps one additional interface. Whatever.

Anyway, definitely not a high priority right now.

gavinking avatar Dec 16 '25 00:12 gavinking