how does the new keyword in hql work?

余生长醉 提交于 2019-12-10 18:15:30

问题


I found this example in jboss's documentation.

select new Family(mother, mate, offspr)
from DomesticCat as mother
    join mother.mate as mate
    left join mother.kittens as offspr

Where does the Family class come from. Do I need to import somewhere, or to use its fully qualified class name?


回答1:


The Family is a plain POJO with the appropriate constructor which either needs to be declared or fully qualified.




回答2:


Family must be a Java object that has an appropriate constructor. The import is optional if the object is a normal mapped entity (either using using annotations or in a hibernate mapping XML).




回答3:


The import is optional, if it is found for another reason. :-)

Yes, it needs to exist. It is a class that is not an entity, you create it to handle in a intelligible way the three columns. It needs a constructor appropriate for the call.

Basically, it is like if you did:

  • a query that returns List (with the three columns)
  • loop on the list, creates a Family object holding the columns, and return the list of these results


来源:https://stackoverflow.com/questions/2145693/how-does-the-new-keyword-in-hql-work

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