1

I am using MySQL database and Spring Data. Every time I try to save data, I got an error

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

After getting this error after compile, I found two new entities added inside the database: "image_path" and "upload_date". I have not written any code to do such thing.

I compile again to see that the image_path column has being inserted with the data that the ImagePath entity is suppose to.

Entity

@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*
}

Thymeleaf Form

<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>  

Controller

    @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 Data Repository

public interface PhotoRepository extends JpaRepository<Photo, Integer> {

}

SQl Database

Why are the two new columns created automatically in the database ?

I expect the new incoming data to be saved in the annotated "ImagePath" and "UploadDate" column and not in the new "image_path" and "upload_date" column that it created by itself.

William Harry
  • 33
  • 2
  • 6
  • I am able to understand your question but it doesn't have enough information to help you. Please update your question bit more source code and explanation. – Alexpandiyan Chokkan Jul 16 '19 at 09:01
  • 1
    Your problem is related to JPA naming strategy settings. Read this [question and answer](https://stackoverflow.com/questions/25283198/spring-boot-jpa-column-name-annotation-ignored). – Johna Jul 16 '19 at 09:06
  • @Johna Thanks for the solution and pointing out the cause of the problem. – William Harry Jul 16 '19 at 09:22

1 Answers1

2

hibernate's default naming strategy maps name of fields to columns in DB by replacing upper case letter with lower + _. to override this strategy (in spring boot) you can use this property

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

for more details have a look at this link

Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43