|
18 | 18 | import jakarta.persistence.Subgraph;
|
19 | 19 | import jakarta.persistence.TypedQuery;
|
20 | 20 |
|
| 21 | +import jakarta.persistence.metamodel.EntityType; |
21 | 22 | import org.hibernate.engine.spi.SessionImplementor;
|
| 23 | +import org.hibernate.graph.internal.RootGraphImpl; |
22 | 24 | import org.hibernate.graph.spi.GraphImplementor;
|
23 | 25 | import org.hibernate.graph.spi.RootGraphImplementor;
|
| 26 | +import org.hibernate.metamodel.RepresentationMode; |
| 27 | +import org.hibernate.metamodel.model.domain.EntityDomainType; |
24 | 28 | import org.hibernate.query.SelectionQuery;
|
25 | 29 |
|
26 | 30 | /**
|
|
33 | 37 | */
|
34 | 38 | public final class EntityGraphs {
|
35 | 39 |
|
| 40 | + /** |
| 41 | + * Create a new entity graph rooted at the given entity, without |
| 42 | + * needing a reference to the session or session factory. |
| 43 | + * |
| 44 | + * @param rootType The {@link EntityType} representing the root |
| 45 | + * entity of the graph |
| 46 | + * @return a new mutable {@link EntityGraph} |
| 47 | + * |
| 48 | + * @since 7.0 |
| 49 | + */ |
| 50 | + public static <T> EntityGraph<T> createGraph(EntityType<T> rootType) { |
| 51 | + return new RootGraphImpl<>( null, (EntityDomainType<T>) rootType ); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Create a new entity graph rooted at the given |
| 56 | + * {@linkplain RepresentationMode#MAP dynamic entity}, without |
| 57 | + * needing a reference to the session or session factory. |
| 58 | + * |
| 59 | + * @param rootType The {@link EntityType} representing the root |
| 60 | + * entity of the graph, which must be a dynamic |
| 61 | + * entity |
| 62 | + * @return a new mutable {@link EntityGraph} |
| 63 | + * |
| 64 | + * @since 7.0 |
| 65 | + */ |
| 66 | + public static EntityGraph<Map<String,?>> createGraphForDynamicEntity(EntityType<?> rootType) { |
| 67 | + final EntityDomainType<?> domainType = (EntityDomainType<?>) rootType; |
| 68 | + if ( domainType.getRepresentationMode() != RepresentationMode.MAP ) { |
| 69 | + throw new IllegalArgumentException( "Entity '" + domainType.getHibernateEntityName() |
| 70 | + + "' is not a dynamic entity" ); |
| 71 | + } |
| 72 | + @SuppressWarnings("unchecked") //Safe, because we just checked |
| 73 | + final EntityDomainType<Map<String, ?>> dynamicEntity = (EntityDomainType<Map<String, ?>>) domainType; |
| 74 | + return new RootGraphImpl<>( null, dynamicEntity ); |
| 75 | + } |
| 76 | + |
36 | 77 | /**
|
37 | 78 | * Merges multiple entity graphs into a single graph that specifies the
|
38 | 79 | * fetching/loading of all attributes the input graphs specify.
|
|
0 commit comments