简体   繁体   中英

Hibernate execute Sql Server procedure

I have tried a lot of ways to call a simple stored procedure com hibernate and couldn´t just find a way to that.

I have a procedure that return a local table variable:

SET NOCOUNT ON
declare @tableTemp Table
(
    ID int,
    Tipo nvarchar(50),
    Regra nvarchar(50),
    Quantidade int
)

select * from @tableTemp

And my query is:

Query query = em.createNativeQuery("{call GetEstatisticasDeRegras}");
List result =  query.getResultList();

I keep getting the error:

 javax.persistence.PersistenceException: org.hibernate.MappingException: **No Dialect mapping for JDBC type: -9**

Have alredy tried:

*call GetEstatisticasDeRegras

*exec GetEstatisticasDeRegras

*execute GetEstatisticasDeRegras

*{call GetEstatisticasDeRegras}

*exec dbo.GetEstatisticasDeRegras

But not way. My user has all privilages over the procedure.


EDIT

My persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="SIPAAM_WS_2">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>Model.DadosPreProcessamento.Evento</class>
        <class>Model.DadosPreProcessamento.Servico</class>
        <class>Model.DadosPreProcessamento.DadosPreProcessamento</class>
        <class>Model.Jobs.Job</class>
        <class>Model.Jobs.JobFiltro</class>
        <class>Model.Jobs.JobResultado</class>
        <class>Model.Jobs.JobResultadoDados</class>
        <class>Model.Jobs.JobResultadoPontuacao</class>
        <class>Model.Jobs.JobEstatisticas</class>
        <class>Model.Usuario.Usuario</class>

        <properties>
            <property name="hibernate.connection.url"
                value="jdbc:sqlserver://localhost;databaseName=SIPAAM;" />
            <property name="hibernate.connection.username" value="x" />
            <property name="hibernate.connection.password" value="x" />
            <property name="hibernate.connection.driver_class" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

Any help with this?

This error occurs because the hibernate does not know how to mapping nvarchar database types to java type. You need to define your Dialect . Suppose that you use sqlserver2008, You can define Dialect like this

public class MsSqlDialect extends SQLServer2008Dialect {
    public MsSqlDialect(){
        super();
        registerHibernateType(Types.NVARCHAR, "string");
    }
}

Then modify hibernate.dialect to your own

<property name="hibernate.dialect" value="xxx.xxxx.MsSqlDialect" />

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