简体   繁体   中英

Spring Data JPA filtering

I have a controller which is like below.

public String getAccountsFilter(
    @PathVariable("cardHolderId") String cardHolderId,
    @RequestParam(value = "accountType", required = false) String accountType,
    @RequestParam(value = "name", required = false) String name)

The AccountEntity is as below

public class AccountEntity implements Serializable {
    private static final long           serialVersionUID = 1L;
    private String                      id;
    private String                      accounttype;
    private Date                        endDate;
    private boolean                     active;
    private PlanEntity                  planEntity;
    private Set<TransactionEntryEntity> transactionEntry = new HashSet<TransactionEntryEntity>();

I did the filtering for id and accountType which is easy using findByIdAndAccountType() method, and the query is generated automatically.

But if i want to filter using id and name which is property of Plan (AccountEntity.PlanEntity.name) , it is not straightforward since name is from the child table. How can i approach for this criteria?

Assume PlanEntity and TransactionEntryEntity is being annotated by @OneToOne or @OneToMany etc

You may do the following in one of the method in the repository of AccountEntity

@Query("select a from AccountEntity a where a.planEntity.name = :name")
public aMethod( @Param("name") String name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM