вторник, 25 апреля 2017 г.



Полиморфные запросы в HQL

Ситуация:

Имеется суперкласс и несколько его наследников, которые в свою очередь содержат в себе другие объекты (разных типов) с каким-нибудь важным значением (например с датой). Необходимо провести поиск по этому значению. Ваш HQL запрос скорее всего будет выглядеть так:

from Parent
where
    Parent.ValueContainerA.valueName > :someValue
    or
    Parent.ValueContainerB.anotherValueName > :someValue)


Проблема:

Для правильного SQL запроса необходимы два left-join между таблицами Parent, ValueContainerA и ValueContainerB. Нужно чтобы HQL протранслировался в следующее:

SELECT...
FROM Parent
    CROSS JOIN ValueContainerA
    CROSS JOIN ValueContainerB
WHERE
    Parent.ValueContainerA_id = ValueContainerA.id
    OR
    Parent.ValueContainerB_id = ValueContainerB.id

В нашем HQL запросе join делается неявно, и по умолчанию хибернейт среди типов join выбирает inner join, а значит он протранслирует HQL в это:

SELECT ...
FROM Parent
    CROSS JOIN ValueContainerA
    CROSS JOIN ValueContainerB
WHERE
    Parent.ValueContainerA_id = ValueContainerA.id
    AND
    Parent.ValueContainerB_id = ValueContainerB.id

Вместо OR получится AND. Такое условие не сможет быть выполнено, и поэтому в результате найдется 0 строк

Решение:

Указать left join явно:

from Parent
    left join ValueContainerA
    left join ValueContainerB
where
    ValueContainerA.valueName > :someValue
    or
    ValueContainer.B.anotherValueName > :someValue

2 комментария:

  1. The King Casino | Community | Communities | Facebook
    We're the people who provide you the opportunity 더킹카지노 to ラッキーニッキー build a real Vegas experience for 10bet yourself and your guests. We are the people who have the

    ОтветитьУдалить
  2. Lawmakers have for years pushed sports betting in the Green Mountain state, however efforts to legalize haven’t gained a lot traction. Oklahoma gaming is dominated 1xbet korea by Native American entities and most seem disinclined to comply with sports betting until different key issues with the state authorities are resolved. Two tribes struck sports betting offers in 2020, however those were later invalidated, part of a bigger battle between competition gaming interests and the government. At one point Missouri seemed just like the surest bet to pass a sports betting bill in 2022, as lawmakers brazenly competed with their counterparts in Kansas to get there first. The tribes, which hold a major lobbying presence in the statehouse, have for years opposed any legislation that wouldn’t give them a monopoly on sports betting.

    ОтветитьУдалить