Browse Source
We now support load-graph / fetch-graph QueryHints on repository query methods, which are applied when a JPA 2.1 capable JPA implementation is used. We explicitly reject the usage of those hints in case the user is running a JPA 2.0 provider. FetchGraphs / LoadGraphs can now be defined on the Entity via the @NamedEntityGraphs annotation. @Entity @QueryEntity @NamedEntityGraphs(@NamedEntityGraph(name = "GroupInfo.members", attributeNodes = @NamedAttributeNode("members"))) public class GroupInfo { @ManyToMany List<GroupMember> members = new ArrayList<GroupMember>(); //default fetch mode is "lazy". } The entity graph "GroupInfo.members" overwrites the fetch-mode of the members collection to be "eager". The entity graph to be used can now configured on a repository query method. @Repository public interface GroupRepository extends CrudRepository<GroupInfo, String> { @EntityGraph("GroupInfo.members") GroupInfo getByGroupName(String name); } The new method JpaQueryMethod#getEntityGraph analyses an @EntityGraph annotation and constructs a new JpaEntityGraph value object that contains the information form the annotation. The new method AbstractJpaQuery#applyEntityGraphConfiguration tries to apply the given EntityGraph configuration if the used JPA persistence provider supports the JPA 2.1 spec. Changed the class path order such that EclipseLink is now placed before the eclipse dependency. EclipseLink references the JPA 2.1 API and allows us to provide type-safe support for the new JPA 2.1 features. Original pull request: #74.pull/71/merge
10 changed files with 440 additions and 22 deletions
@ -0,0 +1,88 @@
@@ -0,0 +1,88 @@
|
||||
/* |
||||
* Copyright 2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.jpa.repository; |
||||
|
||||
import java.lang.annotation.Documented; |
||||
import java.lang.annotation.ElementType; |
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.RetentionPolicy; |
||||
import java.lang.annotation.Target; |
||||
|
||||
import org.springframework.data.annotation.QueryAnnotation; |
||||
|
||||
/** |
||||
* Annotation to configure the JPA 2.1 {@link javax.persistence.EntityGraph}s that should be used on repository methods. |
||||
* |
||||
* @author Thomas Darimont |
||||
* @since 1.6 |
||||
*/ |
||||
@Retention(RetentionPolicy.RUNTIME) |
||||
@Target(ElementType.METHOD) |
||||
@QueryAnnotation |
||||
@Documented |
||||
public @interface EntityGraph { |
||||
|
||||
/** |
||||
* The name of the EntityGraph to use. |
||||
* |
||||
* @return |
||||
*/ |
||||
String value(); |
||||
|
||||
/** |
||||
* The {@link Type} of the EntityGraph to use, defaults to {@link Type#FETCH}. |
||||
* |
||||
* @return |
||||
*/ |
||||
EntityGraphType type() default EntityGraphType.FETCH; |
||||
|
||||
/** |
||||
* Enum for JPA 2.1 {@link javax.persistence.EntityGraph} types. |
||||
* |
||||
* @author Thomas Darimont |
||||
* @since 1.6 |
||||
*/ |
||||
public enum EntityGraphType { |
||||
|
||||
/** |
||||
* When the javax.persistence.loadgraph property is used to specify an entity graph, attributes that are specified |
||||
* by attribute nodes of the entity graph are treated as FetchType.EAGER and attributes that are not specified are |
||||
* treated according to their specified or default FetchType. |
||||
* |
||||
* @see JPA 2.1 Specification: 3.7.4.2 Load Graph Semantics |
||||
*/ |
||||
LOAD("javax.persistence.loadgraph"), |
||||
|
||||
/** |
||||
* When the javax.persistence.fetchgraph property is used to specify an entity graph, attributes that are specified |
||||
* by attribute nodes of the entity graph are treated as FetchType.EAGER and attributes that are not specified are |
||||
* treated as FetchType.LAZY |
||||
* |
||||
* @see JPA 2.1 Specification: 3.7.4.1 Fetch Graph Semantics |
||||
*/ |
||||
FETCH("javax.persistence.fetchgraph"); |
||||
|
||||
private final String key; |
||||
|
||||
private EntityGraphType(String value) { |
||||
this.key = value; |
||||
} |
||||
|
||||
public String getKey() { |
||||
return key; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,77 @@
@@ -0,0 +1,77 @@
|
||||
/* |
||||
* Copyright 2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.jpa.repository.query; |
||||
|
||||
import java.lang.reflect.Method; |
||||
|
||||
import javax.persistence.EntityGraph; |
||||
import javax.persistence.EntityManager; |
||||
import javax.persistence.Query; |
||||
|
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.ClassUtils; |
||||
import org.springframework.util.ReflectionUtils; |
||||
|
||||
/** |
||||
* Customizes a given JPA query with JPA 2.1 features. |
||||
* |
||||
* @author Thomas Darimont |
||||
* @since 1.6 |
||||
*/ |
||||
enum Jpa21QueryCustomizer { |
||||
|
||||
INSTANCE; |
||||
|
||||
private static final Method GET_ENTITY_GRAPH_METHOD; |
||||
private static final boolean JPA21_AVAILABLE = ClassUtils.isPresent("javax.persistence.NamedEntityGraph", |
||||
Jpa21QueryCustomizer.class.getClassLoader()); |
||||
|
||||
static { |
||||
|
||||
if (JPA21_AVAILABLE) { |
||||
GET_ENTITY_GRAPH_METHOD = ReflectionUtils.findMethod(EntityManager.class, "getEntityGraph", String.class); |
||||
} else { |
||||
GET_ENTITY_GRAPH_METHOD = null; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Adds a JPA 2.1 fetch-graph or load-graph hint to the given {@link Query} if running under JPA 2.1. |
||||
* |
||||
* @see JPA 2.1 Specfication 3.7.4 - Use of Entity Graphs in find and query operations P.117 |
||||
* @param em must not be {@literal null} |
||||
* @param query must not be {@literal null} |
||||
* @param entityGraph must not be {@literal null} |
||||
*/ |
||||
public void tryConfigureFetchGraph(EntityManager em, Query query, JpaEntityGraph entityGraph) { |
||||
|
||||
Assert.notNull(em, "EntityManager must not be null!"); |
||||
Assert.notNull(query, "Query must not be null!"); |
||||
Assert.notNull(entityGraph, "EntityGraph must not be null!"); |
||||
|
||||
Assert.isTrue(JPA21_AVAILABLE, "The EntityGraph-Feature requires at least a JPA 2.1 persistence provider!"); |
||||
Assert.isTrue(GET_ENTITY_GRAPH_METHOD != null, |
||||
"It seems that you have the JPA 2.1 API but a JPA 2.0 implementation on the classpath!"); |
||||
|
||||
EntityGraph<?> graph = em.getEntityGraph(entityGraph.getName()); |
||||
|
||||
if (graph == null) { |
||||
return; |
||||
} |
||||
|
||||
query.setHint(entityGraph.getType().getKey(), graph); |
||||
} |
||||
} |
||||
@ -0,0 +1,75 @@
@@ -0,0 +1,75 @@
|
||||
/* |
||||
* Copyright 2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.data.jpa.repository.query; |
||||
|
||||
import javax.persistence.EntityGraph; |
||||
|
||||
import org.springframework.data.jpa.repository.EntityGraph.EntityGraphType; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* EntityGraph configuration for JPA 2.1 {@link EntityGraph}s. |
||||
* |
||||
* @author Thomas Darimont |
||||
* @since 1.6 |
||||
*/ |
||||
public class JpaEntityGraph { |
||||
|
||||
private final String name; |
||||
private final EntityGraphType type; |
||||
|
||||
/** |
||||
* Creates an {@link JpaEntityGraph}. |
||||
* |
||||
* @param name must not be {@null}. |
||||
* @param type must not be {@null}. |
||||
*/ |
||||
public JpaEntityGraph(String name, EntityGraphType type) { |
||||
|
||||
Assert.hasText(name, "The name of an EntityGraph must not be null or empty!"); |
||||
Assert.notNull(type, "FetchGraphType must not be null!"); |
||||
|
||||
this.name = name; |
||||
this.type = type; |
||||
} |
||||
|
||||
/** |
||||
* Returns the name of the {@link EntityGraph} configuration to use. |
||||
* |
||||
* @return |
||||
*/ |
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
/** |
||||
* Returns the {@link EntityGraphType} of the {@link EntityGraph} to use. |
||||
* |
||||
* @return |
||||
*/ |
||||
public EntityGraphType getType() { |
||||
return type; |
||||
} |
||||
|
||||
/* |
||||
* (non-Javadoc) |
||||
* @see java.lang.Object#toString() |
||||
*/ |
||||
@Override |
||||
public String toString() { |
||||
return "JpaEntityGraph [name=" + name + ", type=" + type + "]"; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue