相关疑难解决方法(0)

Spring Data JPA和NamedEntityGraphs

目前我正在努力只能获取我需要的数据.findAll()方法需要根据其被调用的位置获取数据.我不想最终为每个实体图编写不同的方法.此外,我会避免调用实体管理员并自己形成(重复)查询.基本上我想在findAll方法中使用构建,但是使用我喜欢的实体图.任何机会?

@Entity
@Table(name="complaints")
@NamedEntityGraphs({
    @NamedEntityGraph(name="allJoinsButMessages", attributeNodes = {
            @NamedAttributeNode("customer"),
            @NamedAttributeNode("handling_employee"),
            @NamedAttributeNode("genre")
    }),
    @NamedEntityGraph(name="allJoins", attributeNodes = {
            @NamedAttributeNode("customer"),
            @NamedAttributeNode("handling_employee"),
            @NamedAttributeNode("genre"),
            @NamedAttributeNode("complaintMessages")
    }),
    @NamedEntityGraph(name="noJoins", attributeNodes = {

    })
})
public class Complaint implements Serializable{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private long id;

    private Timestamp date;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "customer")
    private User customer;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "handling_employee")
    private User handling_employee;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="genre")
    private Genre genre;

    private boolean closed;

    @OneToMany(mappedBy = "complaint", fetch = …
Run Code Online (Sandbox Code Playgroud)

jpa fetch spring-data-jpa entitygraph

47
推荐指数
4
解决办法
3万
查看次数

Spring Data JPA findAll 与不同的EntityGraph

在 Spring Data JPA 存储库中,我需要指定多个方法来执行相同的操作(例如 findAll),但指定不同的 @EntityGraph 注释(目标是在不同的服务中使用优化的方法)。

Es。

@Repository
public interface UserRepository extends JpaSpecificationExecutor<User>, JpaRepository<User, Long> {

@EntityGraph(attributePaths = { "roles" })
findAll[withRoles](Specification sp);

@EntityGraph(attributePaths = { "groups" })
findAll[withGroups](Specification sp);

etc...
}
Run Code Online (Sandbox Code Playgroud)

在Java中我们不能多次使用同一个方法,那么如何管理它呢?

不使用JPQL也可以吗?

谢谢,

加布里埃尔

java spring spring-data spring-data-jpa entitygraph

8
推荐指数
1
解决办法
3702
查看次数

标签 统计

entitygraph ×2

spring-data-jpa ×2

fetch ×1

java ×1

jpa ×1

spring ×1

spring-data ×1