首页 > 解决方案 > Spring&Hibernate:“字段列表”中的未知列,但列名匹配

问题描述

这是我第一个使用 Spring Boot 的 API。我可以通过在邮递员中执行 POST 请求来进入我的数据库:

{
        "id":"1",
        "type":"postman",
        "cycleDate":"",
        "durationMiliseconds":"",
        "defects":"0"
}

现在,当我发出相同的请求(id = 2)时,我收到一个 500 错误,以及来自我的 API 的以下内容:

引起:java.sql.SQLSyntaxErrorException:'字段列表'中的未知列'cycle_date'

该实体的代码如下:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Test
{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "id", updatable = false, nullable = false)
protected int id;

@Column(name = "type", nullable = false)
protected String type;

@Column(name = "cycle_date")
@Temporal(TemporalType.TIMESTAMP)
protected Date cycleDate;

@Column(name = "duration_milliseconds")
protected int durationMilliseconds;

@Column(name = "defects")
protected int defects;

public Test()
{

}

public Test(int id, String type, Date cycleDate, int durationMilliseconds, int defects)
{
    this.id = id;
    this.type = type;
    this.cycleDate = cycleDate;
    this.durationMilliseconds = durationMilliseconds;
    this.defects = defects;
}

加上吸气剂/二传手。

我已确认我的数据库中的列名为“cycle_date”,没有任何错误。

我确实有另一个继承自这个实体的实体。该实体的目标是表示 Api 测试结果,而超类 Entity 仅表示整体测试或测试运行以及与之相关的某些数据。除了与超类 Test 相关的 Api 之外,还会有其他类型的测试。

@Entity(name = "Api")
public class Api extends Test
{
@Column(name = "uri")
private String uri;

@Column(name = "request_type")
private String requestType;

@Column(name = "request_body")
private String requestBody;

@Column(name = "response_code")
private int responseCode;

@Column(name = "response_body")
private String responseBody;

public Api(int id, String type, Date cycleDate, int durationMilliseconds, int defects)
{
    super(id, type, cycleDate, durationMilliseconds, defects);
}

}

如果我注释掉这个类,我会收到错误:com.mysql.cj.exceptions.WrongArgumentException: SQL String cannot be NULL

这是我使用继承的问题吗?

标签: javaspring-bootentity

解决方案


对于第一个错误 -

  1. 尝试使用 sql 日志记录来查看为 create 语句生成了什么休眠 sql -

    hibernate.show_sql:真

  2. 您可能想研究正在使用的休眠命名策略。

对于第二个错误 - 如果您可以发布可能有帮助的完整堆栈跟踪。

个人建议:由于“post”动词常用于实体的创建,id,不应该在request中传递。相反,它应该是响应和位置标头的一部分。


推荐阅读