hibernate and stored procedure

11,731

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
}
Share:
11,731
user617597
Author by

user617597

Updated on June 26, 2022

Comments

  • user617597
    user617597 almost 2 years

    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