休眠和存储过程 [英] hibernate and stored procedure

查看:117
本文介绍了休眠和存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有人可以告诉我如何在Hibernate中执行以下操作,
这个存储过程返回三个字段

  date,balance,name_of_person 
/ pre>

执行程序'dfd''fdf''34'


  1. 是否需要以这种方式创建bean,以使bean具有以下字段:
    日期,余额,name_of_person


  2. 是否我需要创建属性文件?

  3. 是否有可能使用Criteria在hibernate中执行过程?


  4. 如果我NativeQuery是唯一的选择,那么我如何创建属性文件,因为我没有这样的表作为过程的结果


  5. 是可以单独使用本地查询,而无需使用任何bean或属性文件,并打印结果



解决方案

下面是一个简单的例子:
$ b Hibernate映射文件

 <休眠映射> 
< sql-query name =mySp>
< return-scalar column =datetype =date/>
< return-scalar column =balancetype =long/>
< return-scalar column =name_of_persontype =string/>

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

代码

 列表与LT;为myBean> list = sessionFactory.getCurrentSession()
.getNamedQuery(mySp)
.setParameter(name,name)
.setResultTransformer(Transformers.aliasToBean(MyBean.class))
.list();

Bean类

这个bean拥有存储过程的结果。字段名称必须与Hibernate映射文件中的列名匹配。

  public class MyBean {
private date date;
私人长余额;
私人字符串name_of_person;

// getters and setters
}


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

解决方案

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
}

这篇关于休眠和存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆