Why does using the column name directly in HQL work only occasionally?

I have two HQL queries that I use for a quick and dirty unit test. The first looks something like this:

from Foo where SOME_FOREIGN_KEY = 42

The second is as follows:

from Foo as foo
 inner join foo.Bar as bar
 where foo.SOME_FOREIGN_KEY = 42

The column SOME_FOREIGN_KEY is not the name of what Hibernate knows.

For some reason, the first HQL query works, but the second does not.

My goal is to make the second version work without crossing the graph of objects to the object indicated by the foreign key. For this test, I have a known identifier, and I only need the objects associated with this identifier. The object itself at the other end of the relationship does not matter. Is it possible?

+3
source share
2

- HQL , - .

-, Hibernate WHERE HQL (, , SQL), Hibernate , .

, , Foo TABLE_FOO, HQL

from Foo where SOME_FOREIGN_KEY = 42

SQL

SELECT FROM TABLE_FOO WHERE SOME_FOREIGN_KEY = 42

, TABLE_FOO SOME_FOREIGN_KEY.

, :

from Foo as foo where foo.SOME_FOREIGN_KEY = 42

Hibernate SOME_FOREIGN_KEY Foo, , , .

, , , .

, . , :

from Foo as foo
 inner join foo.Bar as bar
 where SOME_FOREIGN_KEY = 42

, , , , . HQL , .

+6

, Foo , -. , Hibernate "foo". .

, :

      select  f
      from    Foo f
      inner join f.Bar bar
      where   f.SomeForeignKeyId = 42

SomeForeignKeyId - , SOME_FOREIGN_KEY, Id .

Foo, , , . , Eager-, , .

+1

Source: https://habr.com/ru/post/1769200/


All Articles