Can I call a stored procedure with hibernate criteria?
Can I 开发者_StackOverflow中文版use Hibernate criteria to call a stored procudure?
See Using stored procedures for querying in the reference documentation.
Mapped queries are called like this.
List employment = sess.getNamedQuery("BigSP")
.list();
A mapped query can return entities.
<sql-query name="BigSP" callable="true">
<return alias="emp" class="Employment">
<return-property name="employee" column="EMPLOYEE"/>
<return-property name="employer" column="EMPLOYER"/>
<return-property name="startDate" column="STARTDATE"/>
<return-property name="endDate" column="ENDDATE"/>
<return-property name="regionCode" column="REGIONCODE"/>
<return-property name="id" column="EID"/>
<return-property name="salary">
<return-column name="VALUE"/>
<return-column name="CURRENCY"/>
</return-property>
</return>
{ call BigSP }
</sql-query>
No, you need to use a native query. If you are using annotations, see 2.3.2. Mapping native queries.
Below an example:
@Entity
@NamedNativeQuery(
name="baz",
query="call fooProc(:bar, :i)",
callable=true,
readOnly=true,
resultClass=Foo.class
)
public class Foo {
private Date when;
//...
}
And to call it:
@Stateless
public class FooBean implements FooLocal {
@PersistenceContext EntityManager em;
public Foo getAFoo(string bar, int i) {
Foo result = (Foo)em.createNamedQuery("baz").setParameter("bar", bar).setParameter("i", i).getSingleResult();
return result;
}
}
This document describes how to map the result of a stored procedure, executed as a native query.
You can't do it with the Criteria API, but this shouldn't matter.
精彩评论