jakartaee / persistence

https://jakartaee.github.io/persistence/
Other
203 stars 59 forks source link

factory-level access to named queries and named entity graphs #512

Closed gavinking closed 11 months ago

gavinking commented 1 year ago

Introduces NamedQueryReference and:

interface EntityManagerFactory {
    ...

    <R> Map<String, NamedQueryReference<R>> getNamedQueries(Class<R> resultType);
    <E> Map<String, EntityGraph<? extends E>> getNamedEntityGraphs(Class<E> entityType);
}
interface EntityManager {
    ...

    <R> TypedQuery<R> createNamedQuery(NamedQueryReference<R> reference);
}

as proposed in #505.

Also adds typesafe references to named queries and named entity graphs to the canonical metamodel, enabling typesafe code like:

@NamedEntityGraph(name = "withAuthors", ... )
@Entity
class Book { ... }

Book book = em.find(Book_._withAuthors, bookId);

and:

@NamedQuery(name = "byTitle", ... )
@Entity
class Book { ... }

List<Book> books = 
        em.createNamedQuery(Book_._byTitle_)
             .setParameter(1, title)
             .getResultList();
gavinking commented 11 months ago

This pull request is now complete and ready for review. Please let me know your feedback!