Mapeamento de stored procedures com hibernate3

0 respostas
T

Lá pessoal estou desenvolvendo uma aplicaçao com hibernate, neste momento preciso executar uma stored procedure no sqlserver 2005 , utilizando jdbc padrão ja consegui, mas gostaria de mapear para o hibernate com isso acredito que a solução fica mais elegante, além de não precisar implementar nada relativo a pool de conexões para jdbc ja que na configuração do hibernate ja estou usando o c3po…

pesquisei na documentação e econtrei algumas coisas, mas não estou conseguindo sucesso…

segue a baixo a stored procedure…

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[SP_consultaProdutosMobile]

@codProduto char(12),@ean char(16)
as
select
TOP 1
TB1.COD_PRODUTO,
TB1.COD_REF,
TB2.DESC_PRODUTO_EST,
TB1.DESC_GRADE,
TB1.EAN,
TB2.COD_UNIDADE_PRI,
ISNULL(LI.PRECO_V1,0) AS VALOR_PRODUTO,
SUM(ISNULL(SALDO_V1,0)) AS SALDO_ESTOQUE
FROM TBPRODUTOREF TB1
LEFT OUTER JOIN TBPRODUTOSALDO TB0
ON TB1.COD_PRODUTO=TB0.COD_PRODUTO AND
TB1.COD_REF=TB0.COD_REF AND
TB0.COD_FILIAL IN (‘001’,‘002’,‘003’,‘004’)
INNER JOIN TBPRODUTO TB2
ON TB1.COD_PRODUTO=TB2.COD_PRODUTO
LEFT OUTER JOIN TBLISTAPRECOITEM LI
ON TB1.COD_PRODUTO=LI.COD_PRODUTO AND
TB1.COD_REF=LI.COD_REF AND
LI.COD_LISTA= 1
WHERE
(TB0.COD_PRODUTO = @codProduto or @ean = TB1.EAN)
and TB1.STATUS = 'A’
GROUP BY
TB1.COD_PRODUTO,
TB1.COD_REF,
TB2.DESC_PRODUTO_EST,
TB1.DESC_GRADE,
TB1.EAN,
TB2.COD_UNIDADE_PRI,
LI.PRECO_V1
ORDER BY
TB1.COD_PRODUTO,
TB1.COD_REF
GO

SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO

segue o mapeamento xml…

<?xml version=“1.0”?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

“<a href="http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd</a>”>

<hibernate-mapping package=“com.atual.datamodel.mobile.model”>

<class name=“PedidoMobile” table=“tbMobilePedido”>

<id type=“integer” column=“Num_docto”>

<generator class=“native”></generator>

</id>

<property name=“data” type=“timestamp” column=“Data”></property>

<property name=“hora” type=“string” column=“Hora” length=“8”></property>

<property name=“status” type=“string” length=“1” column=“Status”></property>

<property name=“tipo” type=“string” length=“1” column=“Tipo”></property>

</class>

<!–

private List<ItemPedidoMobile> itens = new

ArrayList<ItemPedidoMobile>();

fazer depois

–>
&lt;class name="ItemPedidoMobile" table="tbMobilePedidoItem"&gt;
	&lt;composite-id&gt;
		&lt;key-property name="idItem" column="Num_item" type="integer"&gt;&lt;/key-property&gt;
		&lt;key-property name="idPedido" type="integer" column="Num_docto"&gt;&lt;/key-property&gt;
	&lt;/composite-id&gt;
	&lt;property name="observacao" column="Observacao" type="string"
		length="255"&gt;&lt;/property&gt;
	&lt;property name="Quantidade" column="Quantidade" type="big_decimal"
		precision="20" scale="8"&gt;&lt;/property&gt;
	&lt;component name="produtoDto" class="ProdutoDto"&gt;
		&lt;property name="codProduto" column="Cod_produto" type="string"
			length="12"&gt;&lt;/property&gt;
		&lt;property name="codRef" type="string" length="4" column="Cod_ref"&gt;&lt;/property&gt;
		&lt;property name="codUnidade" type="string" length="4"
			column="Cod_unidade"&gt;&lt;/property&gt;
		&lt;property name="valorProduto" type="big_decimal" precision="15"
			scale="4" column="Valor"&gt;&lt;/property&gt;

		
	&lt;/component&gt;
&lt;/class&gt;



&lt;resultset name="produtoDto"&gt;
	&lt;return alias="produtoDto" class="ItemPedidoMobile"&gt;
		&lt;return-property name="codProduto" column="COD_PRODUTO" /&gt;
		&lt;return-property name="codRegf" column="COD_REF" /&gt;
		&lt;return-property name="descProdutoEst" column="DESC_PRODUTO_EST" /&gt;
		&lt;return-property name="descGrade" column="DESC_GRADE" /&gt;
		&lt;return-property name="codUnidade" column="COD_UNIDADE_PRI" /&gt;
		&lt;return-property name="saldoEstoque" column="SALDO_ESTOQUE" /&gt;
		&lt;return-property name="valorProduto" column="VALOR_PRODUTO" /&gt;
		&lt;/return&gt;
&lt;/resultset&gt;

&lt;sql-query name="SP_consultaProdutosMobile" callable="true"
	resultset-ref="produtoDto"&gt;
	{ call SP_consultaProdutosMobile(?,?)}
&lt;/sql-query&gt;

</hibernate-mapping>

segue q chamada do método em java

public List<ItemPedidoMobile> executaSp(String parametro, String procedure) throws Exception {

try {

HibernateUtil.beginTransaction();

List<ItemPedidoMobile> itens = new ArrayList<ItemPedidoMobile>();
itens = HibernateUtil.getSession().getNamedQuery(procedure).setParameter(0, parametro.trim()).setParameter(1,null).list();
    HibernateUtil.commitTransaction();
    return itens;
}
catch (Exception e) {
    HibernateUtil.rollbackTransaction();
    e.printStackTrace();
}
return null;

por favor caso alguem ja tenha feito algo parecido, me deem uma luz…

parte do log de erro do hibernate

10:31:13,125 DEBUG QueryPlanCache:118 - located native-sql query plan in cache ({ call SP_consultaProdutosMobile(?,?)})

10:31:13,125 DEBUG SessionImpl:1685 - SQL query: { call SP_consultaProdutosMobile(?,?)}

10:31:13,125 DEBUG SQL:401 - /* named native SQL query SP_consultaProdutosMobile <em>/ { call SP_consultaProdutosMobile(?,?)}

Hibernate: /</em> named native SQL query SP_consultaProdutosMobile */ { call SP_consultaProdutosMobile(?,?)}

10:31:13,125 DEBUG StringType:133 - binding ‘99’ to parameter: 1

10:31:13,125 DEBUG StringType:133 - binding ‘99’ to parameter: 2

10:31:13,125 DEBUG JDBCExceptionReporter:69 - could not execute query [{ call SP_consultaProdutosMobile(?,?)}]

com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near ‘{’.

at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)

at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(Unknown Source)

at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(Unknown Source)

at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(Unknown Source)

at com.microsoft.sqlserver.jdbc.TDSCommand.execute(Unknown Source)
Criado 22 de janeiro de 2009
Respostas 0
Participantes 1