Spring criteria query in clause

人盡茶涼 提交于 2021-01-29 13:28:59

问题


I have the following problem with my springboot project:

I'm creating a Criteria Query with the elements of a filter,

this is the code:

    public List<IncidentMinimalPOJO> getPOJOFiltered(FilterPOJO filterValue) {

        List<IncidentMinimalPOJO> resultListPOJO = null;
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Incident> cq = cb.createQuery(Incident.class);

        List<Predicate> predicates = new ArrayList<>();
        Root<Incident> root = cq.from(Incident.class);


        if (filterValue.getInfodesk() != null) {
            Predicate infoDesk = cb.equal(root.get("infoDesk"),filterValue.getInfodesk());
            predicates.add(infoDesk);
        }

        if (filterValue.getAgencyImpact() != null) {
            Predicate agencyImpact = cb.equal(root.get("agencyImpact"),filterValue.getAgencyImpact());
            predicates.add(agencyImpact);
        }

        // this is the wrong part!!!!!!!
        if (filterValue.getTeam() != null) {
            In<Team> teamIn = cb.in(root.get("teams"));
            for (Team team : filterValue.getTeam()) {
                teamIn.value(team);
            }
            predicates.add(teamIn);
        }   

        if (predicates.size() > 0) {
            Predicate dateDeleteNull = cb.isNull(root.get("dateDelete"));
            predicates.add(dateDeleteNull);
            cq.where(predicates.toArray(new Predicate[predicates.size()]));
            TypedQuery<Incident> query = em.createQuery(cq);
            List<Incident> resultList = query.getResultList();
            if (resultList != null) {
                resultListPOJO = new ArrayList<>();
                for (Incident incident : resultList) {
                    IncidentMinimalPOJO incidentPOJO = factoryMinimalPOJO(incident);
                    resultListPOJO.add(incidentPOJO);
                }
                return resultListPOJO;
            } else {
                return new ArrayList<>();
            }
        } else {
            return this.getAllMinimalPOJO();
        }
    }

when the query work with simple class like 'AgencyImpact' (it's a byte) there in no problem, but when I try to make a where clause on an Object like 'Team' i have several error, i can't understand how work criteria query with in clause on a field tha has a ManyToMany relationship.

This is the entity:

@Entity
@NamedQuery(name="Incident.findAll", query="SELECT i FROM Incident i")
public class Incident implements Serializable {

    private static final long serialVersionUID = 769582462646072429L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id_incident")
    private int idIncident;

    private String action;

    private String cause;

    @Lob
    private String note;

    //bi-directional many-to-many association to Team
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(
        name="incident_team"
        , joinColumns={
            @JoinColumn(name="id_incident")
            }
        , inverseJoinColumns={
            @JoinColumn(name="id_team")
            }
        )
    private Set<Team> teams;


i hope it's all almost clear, thank you for the help!

来源:https://stackoverflow.com/questions/60969599/spring-criteria-query-in-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!