1

I've got a simple pojo which is filled thanks to hibernate. I would like to see a variable from this pojo being mapped to a very specific SQL query and I wonder if this is possible to create such a mapping in the hibernate.xml file ?

Thanks a lot

2 Answers 2

1

There's no easy way in pure JPA to do that. The best would be to use Hibernate's @Formula annotation, on which you can specify a "SQL Fragment" that should be used to get the field's data.

Sign up to request clarification or add additional context in comments.

2 Comments

That seems nice, is it possible to do this in the xml mapping file rather than as an annotation ?
0

Are you looking for some thing like this

String sql = "SELECT a.id As id, CONCAT(a.firstName,' ',a.lastName) AS advisorName from tableAdvis";

List<CollectionReport> collectionList = (List<CollectionReport>) getSession().createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(CollectionReport.class)).list();

Here is my CollectionReport POJO looks like

public class CollectionReport {

    private BigInteger id;
    private String advisorCode;
    private String advisorName;    
   //Getter & Setter

}

ResultTransformer will get query result and based on property name in POJO, it assigns values to it.

Comments

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.