Spring Data JPA use Pageable and Predicate filters on a @Query annotated repository method











up vote
0
down vote

favorite












I have a custom DTO :



@Data
@AllArgsConstructor
public class SaleCreditsSearchDto {
private UUID ticketUuid;
private UUID saleUuid;
private String saleType;
private Long ticketNumber;
private LocalDateTime date;
private Double amount;
private Double saleTotalAmount;
private String customerName;
private String accountName;
}


And a query in my Repository that returns me a Page collection of these :



public interface SaleRepository
extends CrudRepository<Sale, UUID> {

@Query("SELECT new com.company.myproject.web.dto.SaleCreditsSearchDto("
+ "ticket.id, sale.id, sale.type, ticket.number, ticket.date, credit.amount, CONCAT(customer.lastname, ' ', customer.firstname), account.title)"
+ " FROM Sale sale"
+ " JOIN sale.ticket ticket"
+ " JOIN sale.credit credit"
+ " JOIN sale.customer customer"
+ " LEFT JOIN sale.account account"
+ " WHERE ticket.idEntity= :identity"
+ " AND ticket.date>= :datebegin"
+ " AND ticket.date< :dateend"
+ " AND ticket.state = 'FINISHED'"
+ " AND credit.initialAmount > 0"
+ " GROUP BY ticket.id, sale.id, credit.initialAmount, customer.lastname, customer.firstname, account.title")
Page<SaleCreditsSearchDto> findTotalCredits(@Param("identity") String identity,
@Param("datebegin") LocalDateTime dateBegin,
@Param("dateend") LocalDateTime dateEnd,
Predicate<SaleCreditsSearchDto> predicate,
Pageable pageable);
}


But @Query methods does not accept more paramaters than @Param and a Pageable object, so I can't pass a Specification implementation, nor a Predicate.



What would you suggest to me to keep returning Page but with the possibility of filtering over some of the fields retrieved in the SELECT clause ?



Implement my own Repository method ? Passing filter in a @Query variable ? Have you practiced something similar before ? Thanks :)










share|improve this question






















  • I would suggest you implement a custom repository and do everything with CriteriaAPI. The problem in your example is that the JPQL query cannot be enhanced with CriteriaAPI predicate because it is executed as it is.
    – Simon Martinelli
    Nov 20 at 16:50










  • Ok that's what I was afraid of.. I will go ahead and write something custom with Criterias. thx for your answer :)
    – Alex
    Nov 20 at 16:53

















up vote
0
down vote

favorite












I have a custom DTO :



@Data
@AllArgsConstructor
public class SaleCreditsSearchDto {
private UUID ticketUuid;
private UUID saleUuid;
private String saleType;
private Long ticketNumber;
private LocalDateTime date;
private Double amount;
private Double saleTotalAmount;
private String customerName;
private String accountName;
}


And a query in my Repository that returns me a Page collection of these :



public interface SaleRepository
extends CrudRepository<Sale, UUID> {

@Query("SELECT new com.company.myproject.web.dto.SaleCreditsSearchDto("
+ "ticket.id, sale.id, sale.type, ticket.number, ticket.date, credit.amount, CONCAT(customer.lastname, ' ', customer.firstname), account.title)"
+ " FROM Sale sale"
+ " JOIN sale.ticket ticket"
+ " JOIN sale.credit credit"
+ " JOIN sale.customer customer"
+ " LEFT JOIN sale.account account"
+ " WHERE ticket.idEntity= :identity"
+ " AND ticket.date>= :datebegin"
+ " AND ticket.date< :dateend"
+ " AND ticket.state = 'FINISHED'"
+ " AND credit.initialAmount > 0"
+ " GROUP BY ticket.id, sale.id, credit.initialAmount, customer.lastname, customer.firstname, account.title")
Page<SaleCreditsSearchDto> findTotalCredits(@Param("identity") String identity,
@Param("datebegin") LocalDateTime dateBegin,
@Param("dateend") LocalDateTime dateEnd,
Predicate<SaleCreditsSearchDto> predicate,
Pageable pageable);
}


But @Query methods does not accept more paramaters than @Param and a Pageable object, so I can't pass a Specification implementation, nor a Predicate.



What would you suggest to me to keep returning Page but with the possibility of filtering over some of the fields retrieved in the SELECT clause ?



Implement my own Repository method ? Passing filter in a @Query variable ? Have you practiced something similar before ? Thanks :)










share|improve this question






















  • I would suggest you implement a custom repository and do everything with CriteriaAPI. The problem in your example is that the JPQL query cannot be enhanced with CriteriaAPI predicate because it is executed as it is.
    – Simon Martinelli
    Nov 20 at 16:50










  • Ok that's what I was afraid of.. I will go ahead and write something custom with Criterias. thx for your answer :)
    – Alex
    Nov 20 at 16:53















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a custom DTO :



@Data
@AllArgsConstructor
public class SaleCreditsSearchDto {
private UUID ticketUuid;
private UUID saleUuid;
private String saleType;
private Long ticketNumber;
private LocalDateTime date;
private Double amount;
private Double saleTotalAmount;
private String customerName;
private String accountName;
}


And a query in my Repository that returns me a Page collection of these :



public interface SaleRepository
extends CrudRepository<Sale, UUID> {

@Query("SELECT new com.company.myproject.web.dto.SaleCreditsSearchDto("
+ "ticket.id, sale.id, sale.type, ticket.number, ticket.date, credit.amount, CONCAT(customer.lastname, ' ', customer.firstname), account.title)"
+ " FROM Sale sale"
+ " JOIN sale.ticket ticket"
+ " JOIN sale.credit credit"
+ " JOIN sale.customer customer"
+ " LEFT JOIN sale.account account"
+ " WHERE ticket.idEntity= :identity"
+ " AND ticket.date>= :datebegin"
+ " AND ticket.date< :dateend"
+ " AND ticket.state = 'FINISHED'"
+ " AND credit.initialAmount > 0"
+ " GROUP BY ticket.id, sale.id, credit.initialAmount, customer.lastname, customer.firstname, account.title")
Page<SaleCreditsSearchDto> findTotalCredits(@Param("identity") String identity,
@Param("datebegin") LocalDateTime dateBegin,
@Param("dateend") LocalDateTime dateEnd,
Predicate<SaleCreditsSearchDto> predicate,
Pageable pageable);
}


But @Query methods does not accept more paramaters than @Param and a Pageable object, so I can't pass a Specification implementation, nor a Predicate.



What would you suggest to me to keep returning Page but with the possibility of filtering over some of the fields retrieved in the SELECT clause ?



Implement my own Repository method ? Passing filter in a @Query variable ? Have you practiced something similar before ? Thanks :)










share|improve this question













I have a custom DTO :



@Data
@AllArgsConstructor
public class SaleCreditsSearchDto {
private UUID ticketUuid;
private UUID saleUuid;
private String saleType;
private Long ticketNumber;
private LocalDateTime date;
private Double amount;
private Double saleTotalAmount;
private String customerName;
private String accountName;
}


And a query in my Repository that returns me a Page collection of these :



public interface SaleRepository
extends CrudRepository<Sale, UUID> {

@Query("SELECT new com.company.myproject.web.dto.SaleCreditsSearchDto("
+ "ticket.id, sale.id, sale.type, ticket.number, ticket.date, credit.amount, CONCAT(customer.lastname, ' ', customer.firstname), account.title)"
+ " FROM Sale sale"
+ " JOIN sale.ticket ticket"
+ " JOIN sale.credit credit"
+ " JOIN sale.customer customer"
+ " LEFT JOIN sale.account account"
+ " WHERE ticket.idEntity= :identity"
+ " AND ticket.date>= :datebegin"
+ " AND ticket.date< :dateend"
+ " AND ticket.state = 'FINISHED'"
+ " AND credit.initialAmount > 0"
+ " GROUP BY ticket.id, sale.id, credit.initialAmount, customer.lastname, customer.firstname, account.title")
Page<SaleCreditsSearchDto> findTotalCredits(@Param("identity") String identity,
@Param("datebegin") LocalDateTime dateBegin,
@Param("dateend") LocalDateTime dateEnd,
Predicate<SaleCreditsSearchDto> predicate,
Pageable pageable);
}


But @Query methods does not accept more paramaters than @Param and a Pageable object, so I can't pass a Specification implementation, nor a Predicate.



What would you suggest to me to keep returning Page but with the possibility of filtering over some of the fields retrieved in the SELECT clause ?



Implement my own Repository method ? Passing filter in a @Query variable ? Have you practiced something similar before ? Thanks :)







hibernate spring-boot filter spring-data-jpa






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 16:40









Alex

2,4621326




2,4621326












  • I would suggest you implement a custom repository and do everything with CriteriaAPI. The problem in your example is that the JPQL query cannot be enhanced with CriteriaAPI predicate because it is executed as it is.
    – Simon Martinelli
    Nov 20 at 16:50










  • Ok that's what I was afraid of.. I will go ahead and write something custom with Criterias. thx for your answer :)
    – Alex
    Nov 20 at 16:53




















  • I would suggest you implement a custom repository and do everything with CriteriaAPI. The problem in your example is that the JPQL query cannot be enhanced with CriteriaAPI predicate because it is executed as it is.
    – Simon Martinelli
    Nov 20 at 16:50










  • Ok that's what I was afraid of.. I will go ahead and write something custom with Criterias. thx for your answer :)
    – Alex
    Nov 20 at 16:53


















I would suggest you implement a custom repository and do everything with CriteriaAPI. The problem in your example is that the JPQL query cannot be enhanced with CriteriaAPI predicate because it is executed as it is.
– Simon Martinelli
Nov 20 at 16:50




I would suggest you implement a custom repository and do everything with CriteriaAPI. The problem in your example is that the JPQL query cannot be enhanced with CriteriaAPI predicate because it is executed as it is.
– Simon Martinelli
Nov 20 at 16:50












Ok that's what I was afraid of.. I will go ahead and write something custom with Criterias. thx for your answer :)
– Alex
Nov 20 at 16:53






Ok that's what I was afraid of.. I will go ahead and write something custom with Criterias. thx for your answer :)
– Alex
Nov 20 at 16:53



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397585%2fspring-data-jpa-use-pageable-and-predicate-filters-on-a-query-annotated-reposit%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397585%2fspring-data-jpa-use-pageable-and-predicate-filters-on-a-query-annotated-reposit%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python