Search code examples
javahibernatestored-procedurescriteria

Can I call a stored procedure with hibernate criteria?


Can I use Hibernate criteria to call a stored procudure?


Solution

  • 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>