【问题标题】:Spring & Hibernate: Unknown Column in 'field list' but column name is matchingSpring&Hibernate:“字段列表”中的未知列,但列名匹配
【发布时间】:2021-05-16 23:50:17
【问题描述】:

这是我第一个使用 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;
}

加上 getter/setter。

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

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

@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

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

【问题讨论】:

    标签: java spring-boot entity


    【解决方案1】:

    对于第一个错误 -

    1. 尝试使用 sql 日志记录来查看为 create 语句生成的 hibernate sql -

      hibernate.show_sql: 真

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-19
      • 1970-01-01
      • 2023-03-15
      • 2019-06-30
      • 2021-07-11
      • 2016-02-29
      • 1970-01-01
      相关资源
      最近更新 更多