Home  >  Article  >  How to retrieve restricted entity relationships using Spring JPA?

How to retrieve restricted entity relationships using Spring JPA?

王林
王林forward
2024-02-08 22:20:101163browse

When using Spring JPA for entity relationship retrieval, we sometimes need to limit the results to meet specific business needs. In this article, we will introduce how to use Spring JPA to retrieve restricted entity relationships. By using the query annotations and methods provided by Spring JPA, we can easily implement restrictions on entity relationships, thereby improving query efficiency and accuracy. Whether you are a beginner or an experienced developer, this article will provide you with clear guidance and practical examples to help you better understand and apply Spring JPA's entity relationship retrieval capabilities.

Question content

This is an optimization problem about the round trip between relationships and databases.

tl;dr: You have two entities a and b that have a many-to-many relationship. You need to retrieve a specific subset of an instance of a and its associated b entities. This is the important part, you don't want to retrieve all b entities related to this a instance, but only a subset of them.

Long story

Consider the following entities;

public class a {

  @id
  private long id;

  @manytomany
  private list<b> blist;
}
public class B {

  @Id
  private Long id;

  @ManyToMany
  private List<A> aList;

  private Boolean somePropertyToUseWhileFiltering;
}

I'm trying to retrieve an instance of an entity a and a subset of its related b instances. In my opinion, this can be achieved in three ways;

  1. Get all related b entities while retrieving a, and discard the unnecessary ones.

  2. Make two different repository calls using a lazy relationship: first get an instance of a without an associated instance of b, then get an instance of b specifying the required filters and restrictions.

  3. Write a huge custom jpql or sql query to get a specific subset of a instances and related b instances.

I don't like the first approach at all because it retrieves a lot of unnecessary rows. The third approach is probably the best for complex structures, but why would I use an orm structure in the first place?

In theory all of this should work, I'm currently using the second approach, but I have a concern. Is it harmful to make multiple repository calls to different repositories for a single request? Because I have a complex entity structure with many relationships. I guess this will increase the number of round trips to the database.

Is there any other more suitable way to solve this problem?

Solution

I think you can use nested projections: projection

public interface awithfilteredblistprojection {

    long getid();

    list<bprojection> getfilteredblist();

    interface bprojection {
        string getsomepropertytousewhilefiltering();

        // add other properties from b that you want to include
    }
}

Repository

public interface ARepository extends JpaRepository<A, Long> {

    @Query("SELECT a FROM A a JOIN FETCH a.bList b WHERE a.id = :aId")
    Optional<AWithFilteredBListProjection> findAWithFilteredBList(@Param("aId") Long aId);
}

The above is the detailed content of How to retrieve restricted entity relationships using Spring JPA?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete