为什么 h2 找不到列

Why h2 not found a column

我有以下问题

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Kolumna "CREATED_AT" nie istnieje Column "CREATED_AT" not found; SQL statement: insert into taco (id, created_at, name) values (null, ?, ?) [42122-200]

但在 h2 控制台中,该列存在

我有以下 schema.sql 代码

create table if not exists Taco(
id identity,
name varchar(50) not null,
createdAt timestamp not null
);

和域

package my.taco.models;

import lombok.Data;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;

@Data
@Entity
public class Taco {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @NotNull
    @Size(min=5,message = "Nazwa musi składać się z przynajmniej pięciu znaków")
    private String name;

    private Date createdAt;

    @ManyToMany(targetEntity = Ingredient.class)
    @Size(min=1,message = "Musisz wybrac przynajmniej jeden składnik")
    private List<Ingredient> ingredients;

    @PrePersist
    void createdAt(){
        this.createdAt=new Date();
    }
}

怎么了?

Hibernate 猜测的默认列名(基于属性名)是 CREATED_AT 但你的是 CREATEDAT

您可以重命名数据库中的列(推荐,因为它更标准)或者您可以像这样将列名称指定为 Hibernate :

@Column(name="CREATEDAT")
private Date createdAt;