I create CRUD application. There are two type and category tables:
CREATE TABLE `type` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `sort` varchar(45) NOT NULL, `categoryId` int(11) NOT NULL, PRIMARY KEY (`ID`), KEY `category_idx` (`categoryId`), CONSTRAINT `categoryId` FOREIGN KEY (`categoryId`) REFERENCES `category_production` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION ) CREATE TABLE `category_production` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `category` varchar(45) NOT NULL, PRIMARY KEY (`ID`), UNIQUE KEY `category_UNIQUE` (`category`) ) Deleting, modifying and creating data works correctly. The problem appeared when I wanted to create a category dropdown list for type , i.e. When creating an entry in type should be a list with data from the previous table. The form of the drop-down list itself was created, but there is no data in it. How can this be done?
Type.java
@Entity @Table(name = "type") public class Type implements Serializable{ @Id @Column(name = "ID") @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name = "sort") private String sort; @OneToOne @JoinColumn(name = "categoryId") private Category category; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getSort() { return sort; } public void setSort(String sort) { this.sort = sort; } public Category getCategory() { return category; } public void setCategory(Category category) { this.category = category; } @Override public String toString() { return "Type{" + "id=" + id + ", sort='" + sort + '\'' + ", category=" + category + '}'; } } Category.java
@Entity @Table(name = "category_production") public class Category { @Id @Column(name = "ID") @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name = "category", unique = true) private String name_category; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName_category() { return name_category; } public void setName_category(String name_category) { this.name_category = name_category; } @Override public String toString() { return "Category{" + "id=" + id + ", name_category='" + name_category + '\'' + '}'; } } TypeController.java
@Controller public class TypeController { private TypeService typeService; @Autowired @Qualifier(value = "typeService") public void setTypeService(TypeService typeService) { this.typeService = typeService; } private CategoryService categoryService; @Autowired @Qualifier(value = "categoryService") public void setCategoryService(CategoryService categoryService) { this.categoryService = categoryService; } @RequestMapping(value = "types", method = RequestMethod.GET) public String listType(Model model){ model.addAttribute("type", new Type()); model.addAttribute("listType", this.typeService.listType()); return "types"; } @RequestMapping(value = "/types/add", method = RequestMethod.POST) public String addType(@ModelAttribute("type") Type type,@ModelAttribute("category") Category category){ if (type.getId()==0){ this.typeService.addType(type); this.categoryService.addCategory(category); }else { this.typeService.updateType(type); this.categoryService.updateCategory(category); } return "redirect:/types"; } @RequestMapping("/removetype/{id}") public String removeType(@PathVariable("id") int id){ this.typeService.removeType(id); this.categoryService.removeCategory(id); return "redirect:/types"; } @RequestMapping("/edittype/{id}") public String editType(@PathVariable("id") int id, Model model){ model.addAttribute("type", this.typeService.getTypeById(id)); model.addAttribute("listType", this.typeService.listType()); model.addAttribute("category", this.categoryService.getCategoryById(id)); model.addAttribute("listCategory", this.categoryService.listCategory()); return "types"; } @RequestMapping("typedata/{id}") public String typeData(@PathVariable("id") int id, Model model){ model.addAttribute("type", this.typeService.getTypeById(id)); return "typedata"; } } Part of the html code where I create a drop-down list ( types.jsp ):
<tr> <td><form:label path="category.id">Category:</form:label></td> <td> <form:select path="category.id" cssStyle="width: 150px;"> <form:option value="2">Select a category</form:option> <c:forEach items="${listCategory}" var="category"> <form:option value="${category.id}">${category.name_category}</form:option> </c:forEach> </form:select> </td> </tr> /.../ <c:if test="${!empty listType}"> <table class="tg"> <tr> <th width="60">ID</th> <th width="150">Sort</th> <th width="150">Category</th> <th width="50">Edit</th> <th width="50">Delete</th> </tr> <c:forEach items="${listType}" var="type"> <tr> <td>${type.id}</td> <td><a href="/typedata/${type.id}" target="_blank">${type.sort}</a></td> <td>${category.name_category}</td> <td><a href="<c:url value="/edittype/${type.id}"/>">Edit</a></td> <td><a href="<c:url value="/removetype/${type.id}"/>">Delete</a></td> </tr> </c:forEach> </table> 
/edittype/{id}too? - Sergey Gornostaevcategorycolumn was empty, opening a/edittype/{id}entry appeared and pressing theedit typebutton (to save changes) gave the error: 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 - Mentha