java - 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
这是我使用继承的问题吗?
解决方案
对于第一个错误 -
尝试使用 sql 日志记录来查看为 create 语句生成了什么休眠 sql -
hibernate.show_sql:真
您可能想研究正在使用的休眠命名策略。
对于第二个错误 - 如果您可以发布可能有帮助的完整堆栈跟踪。
个人建议:由于“post”动词常用于实体的创建,id,不应该在request中传递。相反,它应该是响应和位置标头的一部分。
推荐阅读
- android - 在 Android Webview 应用程序中启用上传文件
- html - 为什么 justify-content 不起作用但 align-items 起作用
- wpf - Mahapp Flyout 被 webview2 控件隐藏
- docker - Cant build image for maven:3.6-jdk-8-slim for Flink client and Apple M1 core
- javascript - 在同一服务器和端口上使用 React CDN 和 Django
- google-apps-script - Apps Script JDBC 使用错误的时区读取 DATETIME
- codeception - codeception WebDriver irgnores url 参数
- html - 尝试仅使用 jquery ajax 在 mvc web api 项目中上传图像
- amazon-web-services - 在端点上调用 create_monitoring_schedule 时 Sagemaker 端点无效
- c - 为什么每当我在 VSCode 上运行 mycode 时会出现此“gcc.exe:错误:数组:没有这样的文件或目录”消息?