首页 > 解决方案 > 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”列中。

标签: javamysqlspringhibernatespring-boot

解决方案


hibernate 的默认命名策略通过将大写字母替换为小写 + _ 来将字段名称映射到 DB 中的列。要覆盖此策略(在 Spring Boot 中),您可以使用此属性

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImp

有关更多详细信息,请查看此链接


推荐阅读