简体   繁体   中英

How invoke a Store Procedure using Hibernate con Sql Server 2000?

I have a problem calling store procedure using hibernate session:

Code Java:

@SuppressWarnings("unchecked")
@Transactional(readOnly = true)
@Override
public List<Map<String, Object>> buscarCierre(String vanio) throws Exception {
    try {
        List<Map<String, Object>> mapa = new ArrayList<>();
        String queryString = "usp_Cierre_Dia_Obtener_CierrePorAnio :anio, :pagActual, :tamanio";
        Query query = super.getSession().createSQLQuery(queryString);
        query.setString("anio", vanio);
        query.setInteger("pagActual", 1);
        query.setInteger("tamanio", 1);
        List<Object[]> result = query.list(); // requires casting for generics
        for(Object[] obj : result) {
            System.out.println("--- "+obj[0]);
            System.out.println("*** "+obj[1]);
        }


        return mapa;
    } catch (Exception e) {

        e.printStackTrace();

        throw new Exception( getGenerarError(Thread.currentThread().getStackTrace()[1].getMethodName(), 
                             Constantes.NIVEL_APP_DAO,
                             this.getClass().getName(),
                             e.getMessage()) );
    } 
}

It invoking from SQL Server 2000, is this: usp_Cierre_Dia_Obtener_CierrePorAnio '2014',1,1

Please could help me, and finally also to check if I'm getting the data correctly through a List <Object []>

Error:

Hibernate: usp_Cierre_Dia_Obtener_CierrePorAnio ?, ?, ?
WARN : org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 170, SQLState: 37000
ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Line 1: Incorrect syntax near 'usp_Cierre_Dia_Obtener_CierrePorAnio'.
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:61)

The query string you are using is not correct so Hibernate generated invalid command like : Hibernate: usp_Cierre_Dia_Obtener_CierrePorAnio ?, ?, ?

Refer to Hibernate document to know how to call stored procedures using hibernate

Also there are other examples in SO that shows how we can call stored procedures in Hibernate for example:

Can I call a stored procedure with hibernate criteria?

How can we call a stored procedure with Hibernate and JPA?

you can map a stored procedure to a bean and also don't forget quotes around string argument:

Query query = session.createSQLQuery("EXEC usp_Cierre_Dia_Obtener_CierrePorAnio :anio, :pagActual, :tamanio").addEntity(<MappedClass>.class);
query.setString("anio", "vanio");
query.setInteger("pagActual", 1);
query.setInteger("tamanio", 1);
List<MappedClass> list = query.list();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM