Hibernate执行Sql Server程序

时间:2013-10-26 02:56:33

标签: java hibernate jpa stored-procedures hql

我已经尝试了很多方法来调用一个简单的存储过程来进行休眠,而且无法找到一种方法。

我有一个返回本地表变量的过程:

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

select * from @tableTemp

我的疑问是:

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

我一直收到错误:

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

alredy尝试过:

*致电GetEstatisticasDeRegras

* exec GetEstatisticasDeRegras

*执行GetEstatisticasDeRegras

* {call GetEstatisticasDeRegras}

* exec dbo.GetEstatisticasDeRegras

但不是这样。我的用户对该程序有所有特权。


修改

我的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>

对此有何帮助?

1 个答案:

答案 0 :(得分:0)

发生此错误是因为hibernate不知道如何将nvarchar数据库类型映射到java类型。您需要定义Dialect。假设您使用sqlserver2008,您可以像这样定义Dialect

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

然后将hibernate.dialect修改为您自己的

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