休眠和存储过程 [英] hibernate and stored procedure
问题描述
有人可以告诉我如何在Hibernate中执行以下操作,
这个存储过程返回三个字段
date,balance,name_of_person
/ pre>
执行程序'dfd''fdf''34'
是否需要以这种方式创建bean,以使bean具有以下字段:
日期,余额,name_of_person
是否我需要创建属性文件?
是否有可能使用Criteria在hibernate中执行过程?
如果我NativeQuery是唯一的选择,那么我如何创建属性文件,因为我没有这样的表作为过程的结果
是可以单独使用本地查询,而无需使用任何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'
Whether I need to Create the bean in such a way that the bean has the following fields: date, balance, name_of_person
Whether I need to create the property file?
Is it possible to use Criteria for executing procedures in hibernate?
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
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屋!
查看全文