【问题标题】:Why h2 not found a column为什么h2没有找到一列
【发布时间】:2021-03-15 23:35:47
【问题描述】:

我有以下问题

原因:org.h2.jdbc.JdbcSQLSyntaxErrorException: Kolumna 未找到“CREATED_AT”列“CREATED_AT”; SQL 语句:插入 taco (id, created_at, name) 值 (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();
    }
}

那怎么了?

【问题讨论】:

    标签: java hibernate jpa h2


    【解决方案1】:

    Hibernate 猜测的默认列名(根据属性名)是CREATED_AT,但你的是CREATEDAT

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-11
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多