问题描述
我尝试从MSSQL Server 2016数据库中的现有表中读取.如果我验证(hbm2ddl.auto->验证),则会收到异常架构验证:缺少表".
I try to read from an existing table in a MSSQL Server 2016 Database. If I validate (hbm2ddl.auto --> validate) I get an Exception "Schema-validation: missing table".
我比较了生成的Hibernate SQL代码(如果我从验证切换到使用我可以在MSSQL Server本身中生成的SQL代码进行更新.它看起来是相等的:
I compared the generated Hibernate SQL Code (if I switch from validate to update with the SQL Code which I can generate in MSSQL Server itself. It looks equal:
-- generated SQL from Hibernate
create table Artikel (
Artnr int not null,
Artgruppe CHAR(5),
Bezeichnung VARCHAR(30) not null,
EPreis money,
primary key (Artnr)
)
-- generated SQL from MSSQL Server 2016
CREATE TABLE [dbo].[Artikel](
[Artnr] [int] NOT NULL,
[Bezeichnung] [varchar](30) NOT NULL,
[EPreis] [money] NULL,
[Artgruppe] [char](5) NULL,
PRIMARY KEY CLUSTERED
(
[Artnr] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Java模型是:
@Entity
@Table(name = "Artikel")
public class Article {
@Id
@Column(name = "Artnr", nullable=true)
private int article_id;
@Column(name = "Bezeichnung", columnDefinition="VARCHAR(30)", nullable=false)
private String description;
@Column(name = "EPreis", columnDefinition = "money", nullable=true)
private BigDecimal price;
@Column(name = "Artgruppe", columnDefinition="CHAR(5)")
private String article_group;
// getters and setters
// ... not shown here
}
那是什么问题?也许与货币类型有关?
So what is the problem? Maybe it has something to do with the money type?
感谢和问候
推荐答案
此处有2个问题:表/列名称的大小写敏感性和命名策略.
请注意错误消息中的表名,并注意大小写字母.当我遇到这个问题时,我的假设是表名不区分大小写.至少当我编写sql查询时就是这种情况(我当时使用的是MSSQL).但是显然,JPA确实关心大小写.
Be careful with what the table name is in your error message, and pay attention to upper and lower case letters. When I faced this problem , my assumption was that table names were case insensitive. At least that's the case when I write an sql query (I was using MSSQL in my case) . But apparently, JPA does care about case sensitivity.
关于命名策略,如果您未指定任何策略,则JPA会将表,带有一些大写字母的列名称(例如 User_Detail
)转换为所有小写和下划线分隔的名称(即 dbo.user_detail
).如果要在批注中提供表名和列名,并且不希望JPA转换它们,则将这两行添加到 application.properties
:
About the naming strategy, if you don't have any strategy specified, JPA is transforming table, column names with some upper case letters (e.g User_Detail
) to all lower-case and underscore separated names (i.e dbo.user_detail
). If you are providing you table and column names in annotations, and don't want JPA to transform them, then add these 2 lines to your application.properties
:
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
这篇关于休眠:模式验证:缺少表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!