春季启动和H2表人口错误:org.hibernate.tool.schema.schema.schema.commandaccepteanceException:执行DDL的错误

发布于 2025-01-29 01:33:03 字数 2085 浏览 1 评论 0原文

我在内存数据库中填充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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫罗兰の梦幻 2025-02-05 01:33:03

在实体类中,您已经在骆驼案例IE构建代码中定义了列名,

@Id
@Column(name = "BuildingCode")
private String id;

这将使JPA期望在db表中的列名building_code,如果它找不到一个列,它将创建名为buildent_code nature building_code,nature buildure_code,具有数据大小varchar(255)和“不是零”。

在您的插入查询中,它具有用于建筑代码列的值,但对于新创建的building_code列没有值,因此错误 -

**NULL not allowed for column "BUILDING_CODE"**

同样适用于其他骆驼式列的列,即。

BuildingName, 
BuildAbr,
SquareFt,
AssetId 

结论:如果您不希望JPA使用下划线创建列,请在骆驼案中的实体中提及列名。
如果您更改列定义如下:

@Id
@Column(name = "Buildingcode") //change camel case to title case
private String id;

但是,我建议在创建表中更改列名,并插入表脚本以下如下:

    create table property(
      latitude        NUMERIC(9,6) NOT NULL,
      longitude       NUMERIC(10,6) NOT NULL,
      building_code   VARCHAR(5) NOT NULL PRIMARY KEY,
      building_name   VARCHAR(42) NOT NULL,
      build_abr       VARCHAR(18) NOT NULL,
      address         VARCHAR(42) NOT NULL,
      square_ft       INTEGER,
      asset_id        INTEGER
    );
        
    insert into property 
    ( latitude, 
         longitude,
         building_code,
         building_name,
         build_abr,
         address,
         square_ft,
         asset_id) 
    values (43.453696,
         -76.544895,
         '0006',
         'Lanigan Hall',
         'LANIGAN-6',
         'some address',
         88200,
         1743);

In Entity class you have defined column name in camel case i.e. BuildingCode,

@Id
@Column(name = "BuildingCode")
private String id;

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 -

**NULL not allowed for column "BUILDING_CODE"**

Same is applicable for other Camel-cased columns as well, viz.

BuildingName, 
BuildAbr,
SquareFt,
AssetId 

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:

@Id
@Column(name = "Buildingcode") //change camel case to title case
private String id;

But, instead I would suggest simply changing column names in create table and insert table scripts to have underscore as below:

    create table property(
      latitude        NUMERIC(9,6) NOT NULL,
      longitude       NUMERIC(10,6) NOT NULL,
      building_code   VARCHAR(5) NOT NULL PRIMARY KEY,
      building_name   VARCHAR(42) NOT NULL,
      build_abr       VARCHAR(18) NOT NULL,
      address         VARCHAR(42) NOT NULL,
      square_ft       INTEGER,
      asset_id        INTEGER
    );
        
    insert into property 
    ( latitude, 
         longitude,
         building_code,
         building_name,
         build_abr,
         address,
         square_ft,
         asset_id) 
    values (43.453696,
         -76.544895,
         '0006',
         'Lanigan Hall',
         'LANIGAN-6',
         'some address',
         88200,
         1743);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文