This is my Book class:
@Entity
@Table(name="book")
public class Book {
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@ManyToOne(targetEntity=Category.class,cascade=CascadeType.ALL,fetch=FetchType.LAZY)
@JoinColumn(name="CategoryId")
public Category category;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(length=10)
private int book_id;
@Column(length=128)
private String title;
@Column(length=64)
private String author;
@Column(length=200)
private String description;
@Column(length=10)
private int ISBN;
@Column(length=10)
private float price;
private Date published_Date;
@Lob
@Column
@Basic(fetch = FetchType.LAZY)
private byte[] icon;
//getter and setter
}
This is my Category class:
@Entity
@Table(name="category1")
public class Category {
@Id
@Column(length=12)
@GeneratedValue(strategy=GenerationType.AUTO)
public int CategoryId;
@Column(length=50)
public String CategoryName;
//@JsonBackReference
@OneToMany(mappedBy="category")
private List<Book> books = new ArrayList<Book>();
//getter and setter
}
The relationship between them is one to many. This is my Category Service class
@Service
@Transactional
public class AdminServiceImpl implements AdminService {
@Autowired
private CategoryDao dao;
@Autowired
private BookDao dao1;
@Override
public List<Category> getAllCategory(){
return dao.findAll();
}
}
My Controller class
@RestController
@RequestMapping("/bookstore")
public class CategoryController {
@Autowired
private AdminService service;
@GetMapping("/GetAllCategory")
private ResponseEntity<List<Category>> getAllCategory() {
List<Category> catlist = service.getAllCategory();
return new ResponseEntity<List<Category>>(catlist, new HttpHeaders(), HttpStatus.OK);
}
}
My category table already has data.When i try to display them it is showing double values. Displaying values using Postman The Category table in the Database: Database table
Jackson's ObjectMapper uses the Java bean pattern and it expects the following
The getters and setters start with get and set, respectively, followed by the corresponding field name with its first letter capitalized.
Change
CategoryId
tocategoryId
(first letter lowercase)and
CategoryName
tocategoryName