java - Spring Data 未保存在带注释的列名中
问题描述
我正在使用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: HY000
2019-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 value
org.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>
控制器
@PostMapping(value="/addPost")
public String upload(@RequestParam MultipartFile file,
HttpSession session,
@ModelAttribute ("photo") Photo photo ){
String path=session.getServletContext().getRealPath("/");
String filename=file.getOriginalFilename();
String savedPath = (path + filename);
try{
byte barr[]=file.getBytes();
BufferedOutputStream bout=new BufferedOutputStream(
new FileOutputStream(path+"/"+filename));
photo.setAlbumID(1);
photo.setImagePath(savedPath);
photo.setLocationID(1);
photoService.save(photo);
bout.write(barr);
bout.flush();
bout.close();
}catch(Exception e){System.out.println(e);}
//return new ModelAndView("upload-success","filename",path+"/"+filename);
return "/user";
}
Spring 数据存储库
public interface PhotoRepository extends JpaRepository<Photo, Integer> {
}
为什么会在数据库中自动创建两个新列?
我希望新的传入数据保存在带注释的“ImagePath”和“UploadDate”列中,而不是它自己创建的新“image_path”和“upload_date”列中。
解决方案
hibernate 的默认命名策略通过将大写字母替换为小写 + _ 来将字段名称映射到 DB 中的列。要覆盖此策略(在 Spring Boot 中),您可以使用此属性
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImp
有关更多详细信息,请查看此链接
推荐阅读
- javascript - 打字稿从其他属性的类型推断道具类型
- css - 安装和卸载组件时带有反应的滑入和滑出动画
- r - 在 R 中使用 API 连接从coronavirus.gov.uk 下载多个区域的数据
- html - 哪种编码语言允许在不更改 URL 的情况下更改此页面的内容?
- python - 为什么我的 python lambda 函数不起作用?
- java - MongoDB 动态行号创建
- reactjs - 在 props.history.push 获取 react typescript 和 react router dom 的克隆错误
- javascript - 当用户使用网络浏览器时,如何使用 react native 获取用户访问的所有 URL?
- android - 安卓开发教程。Linux 上的 Android Studio。启动模拟器崩溃或挂起,然后冻结计算机,我需要硬重启
- php - 按存在于另一个数组或集合中的属性值对集合进行排序