我正在开发通过Hibernate框架访问运行SQL Server 2012的数据库的应用程序。但是,我不知道如何使用序列来生成要添加的新记录的ID。每当我尝试将新的对象实例保存到数据库表时,都会出现异常。要保存的类如下:
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;
}
}
如何更改Spring用来通过Spring MVC 5 Web App查询Microsoft SQL Server序列的SQL查询?
由于错误,我无法将所有Microsoft SQL Server序列与Spring MVC 5 Web App的Hibernate一起使用
Invalid object name <sequence>.
该问题影响Microsoft SQL Server 2012(但也影响Microsoft SQL Server 2018)。
我在Spring Tool Suite 4中将Spring MVC 5框架与Hibernate一起使用。 该服务器是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 Status 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$1$1 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 Status 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];
用户的身份验证类型为“ SQL Server身份验证”。 用户具有“公共”角色。
我无法弄清楚如何使用序列来生成要添加的新记录的ID;每当我尝试将新的对象实例保存到数据库表时,都会出现异常。
请帮助。 提前谢谢了。 操作系统:Microsoft Windows 10 64bit版本2019.3
答案 0 :(得分:0)
我找到了一个解决方案: 您需要在“ 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