我正在使用MySQL数据库和 Spring Data。每次我尝试保存数据时,都会出错2019-07-16 15:35:54.590 WARN 8972 --- [nio-8090-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1364, SQLState: HY0002019-07-16 15:35:54.591 ERROR 8972 --- [nio-8090-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper : Field 'ImagePath' doesn't have a default valueorg.springframework.orm.jpa.JpaSystemException: could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement编译后出现此错误后,我发现数据库中添加了两个新实体:“image_path”和“upload_date”。我没有写任何代码来做这样的事情。我再次编译以查看 image_path 列已插入 ImagePath 实体应该插入的数据。实体@Entity@Table(name="photo")public class Photo { @Id @Column(name="id") @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(name="albumID") private int albumID; @Column(name="LocationID") private int locationID; @Column(name="Title") private String title; @Column(name="Description") private String description; @Column(name="UploadDate") private Timestamp uploadDate; @Column(name="ImagePath") private String imagePath;*ommited getters and setters for abbreviation*}百里香叶形式<form action="#" th:action="@{/addPost}" th:object="${photo}" method="post" enctype="multipart/form-data"> Select File: <input type="file" name="file"/> <input type="text" th:field="*{title}" class="form-control mb-4 col-4" placeholder="Title of the Photo"> <input type="text" th:field="*{description}" class="form-control mb-4 col-4" placeholder="Description"> <input type="submit" value="Upload File"/> </form> 为什么在数据库中自动创建两个新列?我希望新的传入数据保存在带注释的“ImagePath”和“UploadDate”列中,而不是保存在它自己创建的新“image_path”和“upload_date”列中。
1 回答
繁星coding
TA贡献1797条经验 获得超4个赞
hibernate 的默认命名策略通过将大写字母替换为小写字母 + _,将字段名称映射到 DB 中的列。要覆盖此策略(在 spring boot 中),您可以使用此属性
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImp
添加回答
举报
0/150
提交
取消