春季启动和H2表人口错误:org.hibernate.tool.schema.schema.schema.commandaccepteanceException:执行DDL的错误
我在内存数据库中填充H2很难填充。这里的想法是将建筑代码作为主要键和ID唯一的构建代码。
我得到以下类级别错误:
org.hibernate.tool.schema.schema.schema.schema.commandaccepteranceException:错误执行ddl“ Alter表属性添加列buildding_code building_code varchar(255)不是null“通过jdbc语句
由:org.h2.jdbc.jdbc.jdbcsqlintegrityconstratectrationexception:null不允许列“ building_code”; SQL语句:
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "Property")
public class Property {
@Id
@Column(name = "BuildingCode")
String id;
@Column(name = "Latitude")
Float latitude;
@Column(name = "Longitude")
Float longitude;
@Column(name = "BuildingName")
String buildingName;
@Column(name = "BuildAbr")
String buildAbr;
@Column(name = "Address")
String address;
@Column(name = "SquareFt")
Long squareFt;
@Column(name = "AssetId")
Long assetId;
public String getLatLong(){
return "[" + this.getLatitude() + "," + this.getLongitude() + "]";
}
}
表创建和示例插入
CREATE TABLE Property(
Latitude NUMERIC(9,6) NOT NULL
,Longitude NUMERIC(10,6) NOT NULL
,BuildingCode VARCHAR(5) NOT NULL PRIMARY KEY
,BuildingName VARCHAR(42) NOT NULL
,BuildAbr VARCHAR(18) NOT NULL
,Address VARCHAR(42) NOT NULL
,SquareFt INTEGER
,AssetId INTEGER
);
INSERT INTO Property(Latitude,Longitude,BuildingCode,BuildingName,BuildAbr,Address,SquareFt,AssetId) VALUES (43.453696,-76.544895,'0006','Lanigan Hall','LANIGAN-6','some address',88200,1743);
也是我的应用程序。
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url = jdbc:h2:mem:testdb:DB_CLOSE_ON_EXIT=FALSE
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.generate_statistics=false
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
I am having trouble getting an H2 In Memory database to populate. The idea here to have the BuildingCode which is unique as a primary key and id.
I get the following class level errors:
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table property add column building_code varchar(255) not null" via JDBC Statement
Caused by: org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column "BUILDING_CODE"; SQL statement:
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "Property")
public class Property {
@Id
@Column(name = "BuildingCode")
String id;
@Column(name = "Latitude")
Float latitude;
@Column(name = "Longitude")
Float longitude;
@Column(name = "BuildingName")
String buildingName;
@Column(name = "BuildAbr")
String buildAbr;
@Column(name = "Address")
String address;
@Column(name = "SquareFt")
Long squareFt;
@Column(name = "AssetId")
Long assetId;
public String getLatLong(){
return "[" + this.getLatitude() + "," + this.getLongitude() + "]";
}
}
Table Creation and Sample Insert
CREATE TABLE Property(
Latitude NUMERIC(9,6) NOT NULL
,Longitude NUMERIC(10,6) NOT NULL
,BuildingCode VARCHAR(5) NOT NULL PRIMARY KEY
,BuildingName VARCHAR(42) NOT NULL
,BuildAbr VARCHAR(18) NOT NULL
,Address VARCHAR(42) NOT NULL
,SquareFt INTEGER
,AssetId INTEGER
);
INSERT INTO Property(Latitude,Longitude,BuildingCode,BuildingName,BuildAbr,Address,SquareFt,AssetId) VALUES (43.453696,-76.544895,'0006','Lanigan Hall','LANIGAN-6','some address',88200,1743);
Also here is my application.properties
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url = jdbc:h2:mem:testdb:DB_CLOSE_ON_EXIT=FALSE
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.generate_statistics=false
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在实体类中,您已经在骆驼案例IE构建代码中定义了列名,
这将使JPA期望在db表中的列名building_code,如果它找不到一个列,它将创建名为buildent_code nature building_code,nature buildure_code,具有数据大小varchar(255)和“不是零”。
在您的插入查询中,它具有用于建筑代码列的值,但对于新创建的building_code列没有值,因此错误 -
同样适用于其他骆驼式列的列,即。
结论:如果您不希望JPA使用下划线创建列,请在骆驼案中的实体中提及列名。
如果您更改列定义如下:
但是,我建议在创建表中更改列名,并插入表脚本以下如下:
In Entity class you have defined column name in camel case i.e. BuildingCode,
This will make JPA to expect a column named building_code in the DB table and if it does not find one, it will create column named building_code with data size varchar(255) and "NOT NULL".
In your insert query, it has value for BuildingCode column but no value for newly created building_code column, hence the error -
Same is applicable for other Camel-cased columns as well, viz.
Conclusion: If you do not want JPA to create columns with underscore, do not mention column names in entity in camel case.
Your code will work if you change column definition as below:
But, instead I would suggest simply changing column names in create table and insert table scripts to have underscore as below: