SQL 服务器 2012 / 2018 - 对象名称无效

SQL Server 2012 / 2018 - Invalid object name

我正在开发一个通过 Hibernate 框架访问数据库 运行 SQL Server 2012 的应用程序。但是,我不知道如何使用我的序列来生成要添加的新记录的 ID;每当我尝试将新对象实例保存到我的数据库 table 时,我都会遇到异常。要保存的class如下:

package com.xantrix.webapp.entities;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "Coupons")
public class Coupons implements Serializable
{
    private static final long serialVersionUID = -2788720560904709897L;

    @Id
    //@GeneratedValue(strategy=GenerationType.TABLE)
    /*
    @TableGenerator(
        name="Coupons",
        table="Progressivi",
        pkColumnName="Tipo",
        valueColumnName="Progressivo",
        allocationSize=200
        )
        */
    @SequenceGenerator(name="Coup_Gen", sequenceName="Test_Seq")
    @GeneratedValue(generator="Coup_Gen")
    //@GeneratedValue(generator="Coupons")
    @Column(name = "Id")
    private long id;

    @Temporal(TemporalType.TIME)
    @Column(name = "Data")
    private Date dataCreaz;

    @Basic
    private int idDeposito;

    @Basic
    private int qta;

    @Basic
    private double valore;

    @ManyToOne
    @JoinColumn(name = "IdCliente", referencedColumnName = "CODFIDELITY")
    private Clienti cliente;

    public Coupons()
    {

    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Date getDataCreaz() {
        return dataCreaz;
    }

    public void setDataCreaz(Date dataCreaz) {
        this.dataCreaz = dataCreaz;
    }

    public int getIdDeposito() {
        return idDeposito;
    }

    public void setIdDeposito(int idDeposito) {
        this.idDeposito = idDeposito;
    }

    public int getQta() {
        return qta;
    }

    public void setQta(int qta) {
        this.qta = qta;
    }

    public double getValore() {
        return valore;
    }

    public void setValore(double valore) {
        this.valore = valore;
    }

    public Clienti getCliente() {
        return cliente;
    }

    public void setCliente(Clienti cliente) {
        this.cliente = cliente;
    }
}

我如何更改 SQL 查询,Hibernate 使用 SQL 查询带有 Spring MVC 5 Web 应用程序的 Microsoft SQL 服务器序列?

由于错误

,我无法将所有 Microsoft SQL 服务器序列与 Spring MVC 5 Web 应用程序的 Hibernate 一起使用
Invalid object name <sequence>.

该问题影响 Microsoft SQL Server 2012(但也影响 Microsoft SQL Server 2018)。

我在 Spring Tool Suite 4 中使用带 Hibernate 的 Spring MVC 5 框架。 服务器是 Apache Tomcat 9.0.19.

我创建了一个名为

的用户
WebClient1

在 SQL Server Management Studio(Microsoft SQL Server 2012)中。

我创建了以下序列:

USE [AlphaShop]
GO

USE [AlphaShop]
GO

/****** Object:  Sequence [dbo].[Test_Seq]    Script Date: 09/07/2019 10:56:28 ******/
CREATE SEQUENCE [dbo].[Test_Seq] 
 AS [bigint]
 START WITH 500
 INCREMENT BY 50
 MINVALUE 500
 MAXVALUE 9223372036854775807
 CACHE 
GO

以下 SQL 查询在 Microsoft SQL Server Management Studio 中成功运行:

SELECT NEXT VALUE FOR dbo.Test_Seq;

但如果我尝试访问 Web 应用程序的以下 URL,则会出现 HTTP 状态 500 错误: http://localhost:8080/alphashop/coupons/aggiungi/67000023 其中文本“67000023”是现有用户的 ID,如以下 SQL 查询的结果所示:

select * from CLIENTI
where CODFIDELITY = 67000023

在 Spring Tool Suite 4 的控制台中,我可以看到最近调用的 SQL 查询:

Hibernate: 
    select
        next_val as id_val 
    from
        Test_Seq with (updlock,
        rowlock)
lug 10, 2019 12:26:38 PM org.hibernate.id.enhanced.TableStructure execute
ERROR: could not read a hi value
com.microsoft.sqlserver.jdbc.SQLServerException: Il nome di oggetto 'Test_Seq' non è valido.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:256)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1621)

lug 10, 2019 12:26:38 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 208, SQLState: S0002
lug 10, 2019 12:26:38 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Il nome di oggetto 'Test_Seq' non è valido.
lug 10, 2019 12:26:38 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [dispatcher] in context with path [/alphashop] threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: error performing isolated work] with root cause
com.microsoft.sqlserver.jdbc.SQLServerException: Il nome di oggetto 'Test_Seq' non è valido.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:256)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1621)

如您所见,Hibernate 生成了错误的 SQL 查询

    select
        next_val as id_val 
    from
        Test_Seq with (updlock,
        rowlock)

而不是:

SELECT NEXT VALUE FOR dbo.Test_Seq;

成功运行。

通过 Web 浏览器 Mozilla Firefox 68.0 在 Apache Tomcat 9 的 HTML 页面中呈现的 HTTP 状态 500 错误是:

Type Exception Report
Message Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: error performing isolated work
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: error performing isolated work
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1013)
Root Cause
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: error performing isolated work
    org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
Root Cause
org.hibernate.exception.SQLGrammarException: error performing isolated work
    org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
    org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
Root Cause
com.microsoft.sqlserver.jdbc.SQLServerException: Il nome di oggetto 'Test_Seq' non è valido.
    com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:256)
    com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1621)

在对象资源管理器中我可以看到序列 "dbo.Test_Seq"。 数据库的名称是 AlphaShop。

我尝试执行以下 SQL 命令,但没有解决问题:

ALTER LOGIN WebClient1 WITH DEFAULT_DATABASE = [AlphaShop];

用户的身份验证类型是"Authentication of SQL Server"。 用户具有角色 "public".

我不知道如何使用我的序列生成要添加的新记录的 ID;每当我尝试将新对象实例保存到我的数据库时,我都会遇到异常 table.

请帮忙。 提前谢谢了。 OS:微软Windows10 64位版本2019.3

我找到了解决办法: 您需要更改 "application.properties" 文件中的 Hibernate 属性以指定正确的方言(在我的例子中,正确的方言是 "SQLServer2012Dialect"):

#Hibernate properties
hibernate.dialect = org.hibernate.dialect.SQLServer2012Dialect
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto = validate

而不是错误的

#Hibernate properties
hibernate.dialect =  org.hibernate.dialect.SQLServerDialect
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto = validate