簡體   English   中英

Spring Hibernate JSP - 嘗試保存記錄時外鍵為null

[英]Spring Hibernate JSP - Foreign key is null when trying to save a record

我有2張桌子 - 食物和Lvl。 每個Lvl都可以儲存很多食物。

Lvl班

@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; }

}

食品類

@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";
    }  
}

和JSP視圖:

<%@ 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>

所以問題是 - 當我嘗試使用JSP添加Food時 - 外鍵(lvl_id)始終為null。 試圖改變JSP中的輸入類型,類中的級聯類型,用@JoinColumn替換mappedby - 沒有結果。 有人能幫助我嗎? 謝謝。

@JoinColumn(name = "lvl_id", nullable = false)
private Lvl lvl_id;

並在對象Food.lvl_id中檢查調試器中是否存在和notnull。

在控制器中嘗試這種變體方法:

@RequestMapping(value = "/addFood", method = RequestMethod.POST)
public String addFood(@ModelAttribute Food food, Model model) {
    foodService.addFood(food);
    return "redirect:/food";
}

同樣在java中,您需要在命名變量中使用駝峰樣式。

lvl_id - > lvlId;)

Lvl類:

@Entity
@Table(name = "Lvl")
public class Lvl implements Serializable {

private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "lvl_seq", strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name = "lvl_seq", sequenceName = "lvl_seq", allocationSize = 1)
    @Column(name = "lvl_id")
    private int lvl_id;

    @Column(name = "dimension")
    private String dimension;

    @Column(name = "title")
    private String title;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumns({ @JoinColumn(name = "lvl_id", referencedColumnName = "lvl_id", nullable = false) })
    @org.hibernate.annotations.Cascade({ org.hibernate.annotations.CascadeType.ALL })
    private List<Food> foodList;

    //getters and setters
}

食品類:

@Entity
@Table(name = "Food")
public class Food implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "food_seq", strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name = "food_seq", sequenceName = "food_seq", allocationSize = 1)
    @Column(name = "food_id")
    private int food_id;

    @Column(name = "location")
    private String location;

    @ManyToOne(optional = false)
    @JoinColumns({ @JoinColumn(name = "lvl_id", referencedColumnName = "lvl_id", insertable = false, updatable = false) })
    private Lvl lvl_id;

}

嘗試以上課程。 它應該工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM