I am trying to do a simple example of one to many mapping in spring boot using this link
https://www.callicoder.com/hibernate-spring-boot-jpa-one-to-many-mapping-example/
I have done as it is. I am the running the application and it is working fine. But when I make a post request to http://localhost:8080/posts it starts giving me errors which I am unable to resolve. The body I include while making a post request is:
{
"title": "Post 1",
"description": "Post 1 Description",
"content": "Post 1 Content"
}
The error is :
2020-06-22 15:57:38.838 WARN 20216 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper :
SQL Error: 1048, SQLState: 23000
2020-06-22 15:57:38.838 ERROR 20216 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper :
Column 'content' cannot be null
2020-06-22 15:57:38.873 ERROR 20216 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] :
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request
processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could
not execute statement; SQL [n/a]; constraint [null]; nested exception is
org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
Post.java:
package com.example.jpa.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
@Entity
@Table(name = "posts")
public class Post extends AuditModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@Size(max = 100)
@Column(unique = true)
private String title;
@NotNull
@Size(max = 250)
private String description;
@NotNull
@Lob
private String content;
public Object getTitle() {
// TODO Auto-generated method stub
return null;
}
public void setTitle(Object title2) {
// TODO Auto-generated method stub
}
public Object getDescription() {
// TODO Auto-generated method stub
return null;
}
public void setDescription(Object description2) {
// TODO Auto-generated method stub
}
public Object getContent() {
// TODO Auto-generated method stub
return null;
}
public void setContent(Object content2) {
// TODO Auto-generated method stub
}
// Getters and Setters (Omitted for brevity)
}
PostController.java:
package com.example.jpa.controller;
import com.example.jpa.exception.ResourceNotFoundException;
import com.example.jpa.model.Post;
import com.example.jpa.repository.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
@RestController
public class PostController {
@Autowired
private PostRepository postRepository;
@GetMapping("/posts")
public Page<Post> getAllPosts(Pageable pageable) {
return postRepository.findAll(pageable);
}
@PostMapping("/posts")
public Post createPost(@Valid @RequestBody Post post) {
return postRepository.save(post);
}
@PutMapping("/posts/{postId}")
public Post updatePost(@PathVariable Long postId, @Valid @RequestBody Post postRequest) {
return postRepository.findById(postId).map(post -> {
post.setTitle(postRequest.getTitle());
post.setDescription(postRequest.getDescription());
post.setContent(postRequest.getContent());
return postRepository.save(post);
}).orElseThrow(() -> new ResourceNotFoundException("PostId " + postId + " not found"));
}
@DeleteMapping("/posts/{postId}")
public ResponseEntity<?> deletePost(@PathVariable Long postId) {
return postRepository.findById(postId).map(post -> {
postRepository.delete(post);
return ResponseEntity.ok().build();
}).orElseThrow(() -> new ResourceNotFoundException("PostId " + postId + " not found"));
}
}
First is column content cannot be null. But as you can see while making a post request content coulmn is not null. Kindly help?