0

My problem statement is :- I want to call a stored procedure in hibernate and i want to map each column to class attributes after performing certain operations on the column returned by the stored procedure.. As any hibernate query return a list of object instead of resultset.. so how can i do it in hibernate... I know in spring we can do it using jdbcTemplate Map row concept easily but I want to use Hibernate only..

More details can be found out in my prev question:- Alternative to NamedParameterJDBC template row mapper in Hibernate

Community
  • 1
  • 1
Ankit Duggal
  • 55
  • 2
  • 13

1 Answers1

1

There are times we have a class, we would like to fill with data according the data returned from a query. The class is a simple POJO and not an Hibernate entity, so Hibernate won’t recognize this class. This can be done in Hibernate by using Transformers.

(UserActivityStat)hibernateSession.createQuery("select count(*) as totalPhotos from Photo p where p.user = :user").setResultTransformer(Transformers.aliasToBean(UserActivityStat.class)).uniqueResult();

In the above example totalPhotos is a property of Class UserActivityStat which is not a HibernateEntity. Using transformers you can achieve your result.

Pratik Shelar
  • 3,154
  • 7
  • 31
  • 51
  • Interesting.. Can you give me more details about this.. and can i retrieve the result from procedure using this.. – Ankit Duggal Oct 21 '13 at 06:27
  • http://stackoverflow.com/questions/4863883/hibernate-mapping-custom-column-names-in-stored-procedure-named-query – Pratik Shelar Oct 21 '13 at 06:33
  • Thanks @Pratik I am trying to implement it – Ankit Duggal Oct 21 '13 at 07:04
  • Hi Pratik I tried to search more about the transformer but stuck in a situation .. I have to Perform operations on the columns depends on that i have to set the bean accordingly.. Can you help me with that.. – Ankit Duggal Oct 21 '13 at 09:24
  • For your situation I would suggest you have a holder dto which holds all the column values retrieved and then pass this object to a service class which will use the column values perform operations on it it and set the beans – Pratik Shelar Oct 21 '13 at 09:30
  • dto is Data Transfer Object a simple POJO with getters and setters to transfer the data. – Pratik Shelar Oct 21 '13 at 10:46