java 休眠和存储过程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5405830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:58:48  来源:igfitidea点击:

hibernate and stored procedure

javahibernatestored-procedures

提问by user617597

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

  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

  1. 我是否需要以这样的方式创建 bean,即 bean 具有以下字段:date、balance、name_of_person

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

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

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

  5. 是否可以单独使用本机查询而不使用任何 bean 或属性文件并打印结果

回答by limc

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.

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

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

    // getters and setters
}