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> {
}
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.