java - Spring Hibernate JSP - Foreign key is null when trying to save a record -
i have 2 tables - food , lvl. on every lvl can stored lot of food.
lvl class
@sequencegenerator(name = "lvl_seq", sequencename = "lvl_seq") @entity @table(name = "lvl") public class lvl { @id @generatedvalue(generator = "lvl_seq") @column(name = "lvl_id") private int lvl_id; @column(name = "dimension") private string dimension; @column(name = "title") private string title; @onetomany(cascade = cascadetype.all, fetch = fetchtype.lazy, mappedby = "lvl_id") private list<food> foodlist; //getset public int getlvl_id() { return lvl_id; } public void setlvl_id(int lvl_id) { this.lvl_id = lvl_id; } public string getdimension() { return dimension; } public void setdimension(string dimension) { this.dimension = dimension; } public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public list<food> getfoodlist() { return foodlist; } public void setfoodlist(list<food> foodlist) { this.foodlist = foodlist; } }
food class
@sequencegenerator(name = "food_seq", sequencename = "food_seq") @entity @table(name = "food") public class food { @id @generatedvalue(generator = "food_seq") @column(name = "food_id") private int food_id; @column(name = "location") private string location; @manytoone @joincolumn(name = "lvl_id") private lvl lvl_id; //getset public int getfood_id() { return food_id; } public void setfood_id(int food_id) { this.food_id = food_id; } public string getlocation() { return location; } public void setlocation(string location) { this.location = location; } public lvl getlvl_id() { return lvl_id; } public void setlvl_id(lvl lvl_id) { this.lvl_id = lvl_id; } }
foodcontroller
@controller public class foodcontroller { @autowired private lvlservice lvlservice; @autowired private foodservice foodservice; @requestmapping("/food") public string listfood(map<string, object> map) { list<integer> lvl = new arraylist<>(); map.put("food", new food()); map.put("foodlist", foodservice.getall()); for(lvl o : lvlservice.getall()) { lvl.add(o.getlvl_id()); } map.put("lvllist", lvl); return "food"; } @requestmapping(value = "/addfood", method = requestmethod.post) public string addfood(@modelattribute("food") food food, bindingresult result) { foodservice.addfood(food); return "redirect:/food"; } @requestmapping("/deletefood/{foodid}") public string deletefood(@pathvariable("foodid") int id) { foodservice.removefood(id); return "redirect:/food"; } }
and jsp view:
<%@ page language="java" contenttype="text/html; charset=utf8" pageencoding="utf8"%> <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf8"> <title><spring:message code="label.bookmark" /></title> </head> <body> <a href="<c:url value="/index" />"> <spring:message code="label.menu" /> </a> <h3><spring:message code="label.foods" /></h3> <form:form method="post" action="addfood" commandname="food"> <table> <tr> <td> <form:label path="location"> <spring:message code="label.location" /> </form:label> </td> <td> <form:input path="location" /> </td> </tr> <tr> <td> <form:label path="lvl_id"> <spring:message code="label.lvl_id" /> </form:label> </td> <td> <form:select path="lvl_id" items="${lvllist}"> </form:select> </td> </tr> <tr> <td colspan="2"> <input type="submit" value="<spring:message code="label.addfood"/>" /> </td> </tr> </table> </form:form> </body> </html>
so problem - when im trying add food using jsp - foreign key (lvl_id) null. tried change input type in jsp, cascade type in class, replace mappedby @joincolumn - no results. can me? thanks.
@joincolumn(name = "lvl_id", nullable = false) private lvl lvl_id;
and check in debugger on exist , notnull in object food.lvl_id .
try variant method in controller:
@requestmapping(value = "/addfood", method = requestmethod.post) public string addfood(@modelattribute food food, model model) { foodservice.addfood(food); return "redirect:/food"; }
also in java need use camel style in naming variables.
lvl_id --> lvlid ;)
Comments
Post a Comment