简体   繁体   English

休眠和存储过程

[英]hibernate and stored procedure

I'm a beginner in hibernate and till this date I have not come across stored procedures. 我是hibernate的初学者,直到这个日期我都没有遇到过存储过程。

Can somebody tell me how to execute the following in Hibernate, this stored procedure returns three fields 有人可以告诉我如何在Hibernate中执行以下操作,这个存储过程返回三个字段

date, balance, name_of_person

execute procedures 'dfd' 'fdf' '34' 执行程序'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 我是否需要以bean具有以下字段的方式创建bean: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? 是否可以使用Criteria在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 如果我是NativeQuery是唯一的选项,那么如何创建属性文件,因为我没有这样的表作为过程的结果

  5. Is it possible to use native query alone without, using any bean or property file, and printing the results 是否可以单独使用本机查询,而不使用任何bean或属性文件,并打印结果

Here's a simple example:- 这是一个简单的例子: -

Hibernate mapping file Hibernate映射文件

<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 Bean类

This bean holds the results from the stored procedure. 此bean保存存储过程的结果。 The field names must match the column names from the Hibernate mapping file. 字段名称必须与Hibernate映射文件中的列名称匹配。

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

    // getters and setters
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM