3

Не могу понять как сделать сравнение в hibernate cruteria. Есть такой метод

@Override
    public List<PollEvent> getPollForUpdate(IncomingSms incommingSMS) {
        Criteria criteria = getSession().createCriteria(PollEvent.class);
        criteria.add(Restrictions.eq("status", 0));
        criteria.add(Restrictions.eq("call.callingnum", incommingSMS.getSourceAddress()));
        return criteria.list();
    }

Он должен вернуть список объектов. Но ворачивает ошибку. ругается на criteria.add(Restrictions.eq("call.callingnum", incommingSMS.getSourceAddress()));

В классе PollEvent есть поле call вот оно

@OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "CALL_ID", nullable = false)
    private Calls call;

а у него есть поле callingnum по которому яи хочу сравнить

@Column(name = "CALLINGNUM", length = 15)
    private String callingnum;

Ошибка которую я получаю

could not resolve property: call.callingnum of: polls.entety.PollEvent

1 ответ 1

3

В этом случае необходимо создать либо вложенный criteria:

Criteria criteria = getSession().createCriteria(PollEvent.class);
criteria.add(Restrictions.eq("status", 0));
criteria.createCriteria("call")
        .add(Restrictions.eq("callingnum", incommingSMS.getSourceAddress()));

либо использовать alias:

Criteria criteria = getSession().createCriteria(PollEvent.class);
criteria.add(Restrictions.eq("status", 0));
criteria.createAlias("call", "c");
criteria.add(Restrictions.eq("c.callingnum", incommingSMS.getSourceAddress()));

Документация

1
  • Нашел решение раньше но ответ правильный. спасибо! 28 дек 2015 в 8:09

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.