Spring Data未保存在带注释的列名中

时间:2019-07-16 08:33:18

标签: java mysql spring hibernate spring-boot

我正在使用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> {

}

SQl Database

为什么在数据库中自动创建了两个新列?

我希望新的传入数据将保存在带有注释的“ ImagePath”和“ UploadDate”列中,而不是保存在它自己创建的新的“ image_path”和“ upload_date”列中。

1 个答案:

答案 0 :(得分:2)

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

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

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