Spring Data JPA,在CrudRepository接口中使用参数@EntityGraph [英] Spring Data JPA, parametrize @EntityGraph in CrudRepository interface
问题描述
Spring Data JPA是否可以执行smth.像这样:
Is it possible with Spring Data JPA to do smth. like this:
public interface UserDao extends CrudRepository<User, Long> {
@EntityGraph(value = :graphName, type = EntityGraph.EntityGraphType.LOAD)
@Query(value = "SELECT DISTINCT u FROM User u")
List<User> findAllWithDetailsByGraphName(@Param(value="graphName") String graphName);
}
是否能够在运行时将graphName传递给方法并使用需要的集合调用负载?此构造无法正常工作,从而产生编译时错误.任何围绕它的游戏也都失败了...
to be able to pass the graphName into method at run-time and invoke the load with a need set of collections? This construction is not working, producing compile time error. Any plays around it also failed...
因此,我在User类中有多个集合,我想根据条件加载这些集合;
So, I have multiple collections in User class, which I want to load on condition;
@Entity
@Table(name="user")
@NamedEntityGraphs({
@NamedEntityGraph(name = "User.details", attributeNodes = {
@NamedAttributeNode("phones"), @NamedAttributeNode("emails"), @NamedAttributeNode("pets")}),
@NamedEntityGraph(name = "User.phones", attributeNodes =
{@NamedAttributeNode("phones")}),
@NamedEntityGraph(name = "User.emails", attributeNodes =
{@NamedAttributeNode("emails")}),
@NamedEntityGraph(name = "User.pets", attributeNodes =
{@NamedAttributeNode("pets")})
})
public class User {
@Id
@GeneratedValue(strategy= GenerationType.AUTO, generator="native")
@GenericGenerator(name = "native", strategy = "native")
@Column(name="user_id")
private Long userId;
@Column(name="name")
private String name;
// more fields omitted
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private Set<Phone> phones;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private Set<Email> emails;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private Set<Pet> pets;
}
现在,我只需要隐式声明所有方法,像这样:
Now, I only must declare all the methods implicitly, like this:
@EntityGraph(value = "User.phones", type = EntityGraph.EntityGraphType.LOAD)
@Query(value = "SELECT DISTINCT u FROM User u")
List<User> findAllWithPhones();
感谢您的建议!
推荐答案
您可以定义一个将实体图作为参数接受的基本JPA存储库.与Specification
结合使用时特别有用.因此,下面是基于Specification
的示例.也可以使用不同类型的参数构造其他查询.
You can define a base JPA repository that accepts the entity graph as a parameter. This is particularly useful in combination with Specification
s. Therefore, here follows an example based on Specification
s. It's also possible to construct other queries, using different types of arguments.
@NoRepositoryBean
public interface MyBaseRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID> {
T findOne(Specification<T> spec, EntityGraphType entityGraphType, String entityGraphName);
List<T> findAll(Specification<T> spec, Sort sort, EntityGraphType entityGraphType, String entityGraphName);
}
实施基本信息库:
@NoRepositoryBean
public class MyBaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements MyBaseRepository<T, ID> {
private EntityManager em;
public MyBaseRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
this.em = entityManager;
}
public MyBaseRepositoryImpl(Class<T> domainClass, EntityManager em) {
super(domainClass, em);
this.em = em;
}
@Override
public T findOne(Specification<T> spec, EntityGraph.EntityGraphType entityGraphType, String entityGraphName) {
TypedQuery<T> query = getQuery(spec, (Sort) null);
query.setHint(entityGraphType.getKey(), em.getEntityGraph(entityGraphName));
return query.getSingleResult();
}
@Override
public List<T> findAll(Specification<T> spec, Sort sort, EntityGraph.EntityGraphType entityGraphType, String entityGraphName) {
TypedQuery<T> query = getQuery(spec, sort);
query.setHint(entityGraphType.getKey(), em.getEntityGraph(entityGraphName));
return query.getResultList();
}
}
指定自定义基础存储库:
Specify the custom base repository:
<jpa:repositories base-package="my.domain" base-class="my.repository.MyBaseRepositoryImpl" />
从自定义基础存储库扩展:
Extend from custom base repository:
public interface UserRepository extends JpaRepository<User, Long>, MyBaseRepository<User, Long>, JpaSpecificationExecutor<User> {
}
使用自定义存储库的方法:
Use the custom repository's method:
Specification mySpecs = ...
List<User> user = picklistRepository.findAll(mySpecs, EntityGraphType.LOAD, "User.phones");
这篇关于Spring Data JPA,在CrudRepository接口中使用参数@EntityGraph的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!