Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate and stored procedure

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

like image 655
user617597 Avatar asked Mar 23 '11 13:03

user617597


1 Answers

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
}
like image 87
limc Avatar answered Oct 01 '22 11:10

limc