I have a problem populating a drop down list with a ManyToOne relationship.
I have two entities, Product referencing Category with Category ID ...
But I can't populate the category list with the Spring form tag and get the pointer to the category in the product table of the database.
Can anyone kindly explain to me how to solve this problem?
PRODUCT CLASS
package it.relazioni.one;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(length = 100, nullable = false)
private String nameProduct;
@Column
private int price;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "idCategory")
private Category category;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNameProduct() {
return nameProduct;
}
public void setNameProduct(String nameProduct) {
this.nameProduct = nameProduct;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
CATEGORY CLASS
package it.relazioni.one;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(length = 45, nullable = false, unique = true)
private String nameCategory;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNameCategory() {
return nameCategory;
}
public void setNameCategory(String nameCategory) {
this.nameCategory = nameCategory;
}
public Category(Integer id, String nameCategory) {
super();
this.id = id;
this.nameCategory = nameCategory;
}
public Category(Integer id) {
super();
this.id = id;
}
public Category(String nameCategory) {
super();
this.nameCategory = nameCategory;
}
}
CONTROLLER CODE
@GetMapping("/createProd")
public ModelAndView createProduct(@ModelAttribute("newProduct") Product p, Model model) {
List<Category> categories = (List<Category>) catRep.findAll();
model.addAttribute("categories", categories);
return new ModelAndView("createProduct", "newProduct", new Product());
}
@PostMapping("/createProductForm")
public ModelAndView createProductForm(@ModelAttribute("newProduct") Product p) {
System.out.println(p.getNameProduct() + " " + p.getCategory());
pdRep.save(p);
return new ModelAndView("createProduct", "newProduct", p);
}
JSP CODE
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form:form action="createProductForm" method="post" modelAttribute="newProduct">
<form:input path="nameProduct"/>
<form:input path="price" type="number"/>
<form:select path="category">
<form:options items="${categories}"></form:options>
</form:select>
<form:button>Create Product!</form:button>
</form:form>
</body>
</html>