6

I'm a beginner in hibernate and till this date I have not come across stored procedures.

Can somebody tell me how to execute the following in Hibernate, this stored procedure returns three fields

date, balance, name_of_person

execute procedures 'dfd' 'fdf' '34'

  1. Whether I need to Create the bean in such a way that the bean has the following fields: date, balance, name_of_person

  2. Whether I need to create the property file?

  3. Is it possible to use Criteria for executing procedures in hibernate?

  4. If I the NativeQuery is the only option, then how can I create the property file as I don't have the such a table as the result from the procedure

  5. Is it possible to use native query alone without, using any bean or property file, and printing the results

Johan
  • 74,508
  • 24
  • 191
  • 319
user617597
  • 788
  • 3
  • 9
  • 21
  • Read the documentation - http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/querysql.html#sp_query – skaffman Mar 23 '11 at 13:21
  • 2
    possible duplicate of [Calling a Stored Procedure in Hibernate](http://stackoverflow.com/questions/3681045/calling-a-stored-procedure-in-hibernate) – Aravind Yarram Mar 23 '11 at 13:24

1 Answers1

6

Here's a simple example:-

Hibernate mapping file

<hibernate-mapping>
    <sql-query name="mySp">
        <return-scalar column="date" type="date" />
        <return-scalar column="balance" type="long" />
        <return-scalar column="name_of_person" type="string" />

        { call get_balance_sp :name }
    </sql-query>
</hibernate-mapping>

Code

List<MyBean> list = sessionFactory.getCurrentSession()
                            .getNamedQuery("mySp")
                            .setParameter("name", name)
                            .setResultTransformer(Transformers.aliasToBean(MyBean.class))
                            .list();

Bean class

This bean holds the results from the stored procedure. The field names must match the column names from the Hibernate mapping file.

public class MyBean  {
    private Date date;
    private Long balance;
    private String name_of_person;

    // getters and setters
}
limc
  • 39,366
  • 20
  • 100
  • 145