Java – can I get the SQL alias of the connection table of Hibernate sqlrestriction?
I have a person class that has an alias for a collection of strings that represent additional names that people may go to For example, Clark Kent may have nicknames "Superman" and "steel man" Dwight Howard also has an alias for Superman
@Entity class Person { @CollectionOfElements(fetch=FetchType.EAGER) Set<String> aliases = new TreeSet<String>();
Hibernate in my database person and person_ Create two tables in aliases Person_ Aliases are people with_ Join table for ID and element columns Suppose person_ Aliases have the following data
-------------------------------- | Person_id | element | -------------------------------- | Clark Kent | Superman | | Clark Kent | Man of Steel | | Dwight Howard | Superman | | Bruce Wayne | Batman | --------------------------------
I want to set a dormancy standard for all people who pass the alias "Superman"
Because the reasons listed here are too long, I really want to use it as a standard query rather than an HQL query (unless an HQL restriction can be added to the criteria object, in which case I am all ears) SQL query Because according to how do I query for objects with a value in a string collection using hibernate criteria?, It's impossible to use the criteria API to refer to the elements of the value type collection. I thought I would add an sqlrestriction on my standard object
Criteria crit = session.createCriteria(Person.class); crit.add(Restrictions.sqlRestriction("XXXXX.element='superman'");
I hope hibernate will create an SQL statement
select * from Person this_ left outer join Person_aliases aliases2_ on this_.id=aliases2_.Person_id where XXXXX.element='superman'
However, I need to use person in the SQL query_ The table alias of the aliases table is filled in xxxxx, which will be "aliases2#" I noticed that if I need to reference the person table alias, I can use {alias} However, this does not work because person is the main table for this condition, not person_ aliases.
What to fill in for xxxxx? If there is no good variable symbol like {alias}, is there any way for hibernate to tell me what the alias is? I noticed a method called generatealias () org hibernate. util. Stringhelper class Does this help me predict what the alias is?
I really want to avoid hard coding 'aliases2_'
Thank you for your time!
Solution
It seems that the criteria API does not allow querying a collection of elements, see hhh-869 (still open) So either try the suggested solution – I don't – or switch to HQL The following HQL query will work:
from Person p where :alias in elements(p.aliases)