Я создал класс dto StockProject, который позволяет получать определенные c элементы из моих продуктов класса сущностей, мне нужно использовать данные фильтра на основе класса dto, поэтому я создал репозиторий jpa с использованием класса dto и я получил нулевое исключение в ответ n. Вот код ниже
Класс Dto
package ma.munisys.dto;
public class StockProjet {
private String annee;
private String client;
private String numLot;
private String commercial;
private Double montant;
private String chefProjet;
public StockProjet(String year, String client,String num_lot, String commercial, String chefProjet, Double montant) {
super();
this.annee = year;
this.client = client;
this.numLot = num_lot;
this.commercial = commercial;
this.montant = montant;
this.chefProjet = chefProjet;
}
public String getAnnee() {
return annee;
}
public void setAnnee(String annee) {
this.annee = annee;
}
public String getClient() {
return client;
}
public void setClient(String client) {
this.client = client;
}
public String getNumLot() {
return numLot;
}
public void setNumLot(String numLot) {
this.numLot = numLot;
}
public String getCommercial() {
return commercial;
}
public void setCommercial(String commercial) {
this.commercial = commercial;
}
public String getChefProjet() {
return chefProjet;
}
public void setChefProjet(String chefProjet) {
this.chefProjet = chefProjet;
}
public Double getMontant() {
return montant;
}
public void setMontant(Double montant) {
this.montant = montant;
}
}
Репозиторий JPA
package ma.munisys.dao;
import java.util.Collection;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import ma.munisys.dto.StockProjet;
@Repository
public interface StockProjetRepository extends JpaRepository<ma.munisys.dto.StockProjet,String>,JpaSpecificationExecutor<ma.munisys.dto.StockProjet> {
@Query("SELECT new ma.munisys.dto.StockProjet(ISNULL(CAST(Year(p.dateCmd) AS string),CAST('0000' AS string)) as annee,p.client ,p.numLot,p.commercial,p.chefProjet,SUM(p.montant) as montant) FROM Produit p GROUP BY p.client,p.numLot,p.commercial,Year(p.dateCmd),p.chefProjet ORDER BY Year(p.dateCmd) DESC")
public Collection<StockProjet> getStockParProjet();
@Query("SELECT new ma.munisys.dto.StockProjet(ISNULL(CAST(Year(p.dateCmd) AS string),CAST('0000' AS string)) as annee,p.client ,p.numLot,p.commercial,p.chefProjet,SUM(p.montant) as montant) FROM Produit p WHERE p.client = :c OR p.numLot = :l OR p.commercial = :co OR CAST(Year(p.dateCmd) AS string )= :y GROUP BY p.client,p.numLot,p.commercial,Year(p.dateCmd),p.chefProjet ORDER BY Year(p.dateCmd) DESC")
public Collection<StockProjet> getStockProjetByFiltre(@Param("l") String numLot,@Param("c") String client,@Param("y") String annee,@Param("co") String commercial);
}
Спецификация dto
package ma.munisys.service;
import org.springframework.data.jpa.domain.Specification;
import ma.munisys.dto.StockProjet;
public class ProduitSpecification2 {
public static Specification<StockProjet> byClient(String client) {
return (root, query, cb) -> {
return cb.equal(root.get("client"), client);
};
}
public static Specification<StockProjet> byNumLot(String numLot) {
return (root, query, cb) -> {
return cb.equal(root.get("numLot"), numLot);
};
}
public static Specification<StockProjet> byAnnee(String annee) {
return (root, query, cb) -> {
return cb.equal(root.get("annee"), annee);
};
}
public static Specification<StockProjet> byNomMagasin(String magasin) {
return (root, query, cb) -> {
return cb.equal(root.get("nomMagasin"), magasin);
};
}
}
И вот что я хочу сделать
public Collection<StockProjet> getStockProjetByFiltre(String numLot, String client, String annee, String commercial){
// aucun filtre
if(numLot.equals("undefined") && client.equals("undefined") && annee.equals("undefined") ){
return stockProjetRepository.getStockParProjet();
}
// filtre par numLot uniquement. E
if(!numLot.equals("undefined") && client.equals("undefined") && annee.equals("undefined") ){
return stockProjetRepository.findAll(ProduitSpecification2.byNumLot(numLot));
}
// filtre par SousDomaine uniquement. F
if(numLot.equals("undefined") && !client.equals("undefined") && annee.equals("undefined") ){
return stockProjetRepository.findAll(ProduitSpecification2.byClient(client));
}
// filtre par annee uniquement. G
if(numLot.equals("undefined") && client.equals("undefined") && !annee.equals("undefined") ){
return stockProjetRepository.getStockProjetByFiltre(numLot,client,annee,commercial);
}
Это исправление и логика c?