0

I know how to do query to resultClass mapping in IBatis.

How can I map the native query result to the object that is a mix of entity class and scalars in hibernate ? How can I set the parameters ?

Please Help.

1 Answer 1

2

With Hibernate Session API, you can do it by comining addEntity() and addScalar() methods:

Query q = s.createSQLQuery(
    "select p.*, count(e.id) as c " +
    "from Project p left join Employee e on p.id = e.project_id " +
    "group by p.id")
    .addEntity(Project.class).addScalar("c");

In JPA you can do it with @SqlResultSetMapping:

@SqlResultSetMappings(
    @SqlResultSetMapping(name = "projectWithCount"
        entities = @EntityResult(entityClass = Project.class),
        columns = @ColumnResult(name = "c")))

...

Query q = s.createSQLQuery(
        "...", "projectWithCount")
Sign up to request clarification or add additional context in comments.

1 Comment

I am new to @SqlResultSetMappings, so can you also tell which class will this annotation entry go to? Will this annotation go into a class which is not an entity class and has 2 properties: 1 pointing to Project class and 1 pointing to int c; ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.