gpt4 book ai didi

Spring Data JPA,在 CrudRepository 接口(interface)中参数化 @EntityGraph

转载 作者:行者123 更新时间:2023-12-04 02:59:06 28 4
gpt4 key购买 nike

Spring Data JPA 是否有可能做到这一点?像这样:

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 传递给方法并使用一组需要的集合调用加载?此构造不起作用,产生编译时错误。围绕它的任何游戏也失败了......

因此,我在 User 类中有多个集合,我想按条件加载它们;
@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;
}

现在,我只需隐式声明所有方法,如下所示:
@EntityGraph(value = "User.phones", type = EntityGraph.EntityGraphType.LOAD)
@Query(value = "SELECT DISTINCT u FROM User u")
List<User> findAllWithPhones();

谢谢你的建议!

最佳答案

您可以定义一个接受实体图作为参数的基本 JPA 存储库。这与 Specification 结合使用特别有用。 s。因此,这里以Specification 为基础的示例。 s。也可以使用不同类型的参数构造其他查询。

@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();
}

}

指定自定义基础存储库:
<jpa:repositories base-package="my.domain" base-class="my.repository.MyBaseRepositoryImpl" />

从自定义基础存储库扩展:
public interface UserRepository extends JpaRepository<User, Long>, MyBaseRepository<User, Long>, JpaSpecificationExecutor<User> {
}

使用自定义存储库的方法:
Specification mySpecs = ...
List<User> user = picklistRepository.findAll(mySpecs, EntityGraphType.LOAD, "User.phones");

关于Spring Data JPA,在 CrudRepository 接口(interface)中参数化 @EntityGraph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50949656/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com