First of all,
select description, weight, owner.name from Dog
invalid SQL. It should be something like
select description, weight, Person.name from Dog join Person on Dog.person_id = Person.id
instead of this. Secondly, why? Although you can do what you want (see below), it is very difficult to do this through the criteria API and you will not get anything to display it. The savings in transferring data for a couple of columns are negligible, unless these columns are huge drops or you select hundreds of thousands of records. In any case, there are more effective ways to solve this problem.
Anywho, in order to do what you want for the criteria, you need to link the linked table (Person) using an alias and specify the forecast for the main criteria using the specified alias:
Criteria criteria = session.createCriteria(Dog.class, "dog") .createAlias("owner", "own") .setProjection( Projections.projectionList() .add(Projections.property("dog.description")) .add(Projections.property("dog.weight")) .add(Projections.property("own.name")) );
Here is the description and example above in the Documentation for the Project Criteria . Keep in mind that when executed, the above criteria will return a list of arrays of objects. You will need to specify ResultTransformer so that the results are converted to real objects.
source share