实现一对一的关系春天休眠

时间:2015-03-19 09:40:35

标签: java spring hibernate orm relational-database

我有一个表cards,它与一对一关系的其他主表(分部和单元)相关。

在UI上我显示下拉菜单以选择卡片的除法和单位值,这是在CardController.java

中创建表单的功能
@RequestMapping(value = "/addCardForm", method = RequestMethod.GET)
    public String addCardForm(ModelMap map)
    {
        map.addAttribute("divisions", divisionService.getAllDivisions());
        map.addAttribute("units", unitService.getAllUnits());
        return "admin/addCard";
    }

addCard.jsp

<form:form  method="post" action="addCard">
<table cellspacing="10" id="card-table">
            <tr>
                <td><label for="division" class="control-label">Division : </label></td>
                <td><select name="division" class="selectpicker">
                    <option>Select</option>
                    <c:forEach items="${divisions}" var="division">
                        <option value="${division.id}">${division.name}</option>
                    </c:forEach>
                </select></td>
            </tr>
            <tr>
                <td><label for="unit" class="control-label">Unit : </label></td>
                <td><select name="unit" class="selectpicker">
                    <option>Select</option>
                    <c:forEach items="${units}" var="unit">
                        <option value="${unit.id}">${unit.name}</option>
                    </c:forEach>
                </select></td>
            </tr>
            <tr>
                <td><button type="submit" class="btn btn-primary">Submit</button></td>
                <td></td>
            </tr>

</table>
</form>

下拉数据填充了数据,但是在提交表单时,将其未设置的分区或单元设置为cardEntity对象,它们被设置为null,这是控制器中的addCard函数:

@RequestMapping(value = "/addCard", method = RequestMethod.POST)
    public String addCard(@ModelAttribute(value="card") CardEntity card, BindingResult result)
    {
        cardService.addCard(card);
        //card.getDivision();  -- this is null
        return "redirect:/card";
    }

除了下拉菜单外,还会在卡片中添加其他字段。

CardEntity.java


@Entity
@Table(name="cards")
@Proxy(lazy=false)
public class CardEntity {

    @Id
    @Column(name="id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @OneToOne
    @JoinColumn(name="division_id", referencedColumnName="id")  
    private DivisionEntity division;


    @OneToOne
    @JoinColumn(name="unit_of_qty_id", referencedColumnName="id")  
    private UnitEntity units;

    public DivisionEntity getDivision() {
        return division;
    }

    public void setDivision(DivisionEntity division) {
        this.division = division;
    }

    public UnitEntity getUnits() {
        return units;
    }

    public void setUnits(UnitEntity units) {
        this.units = units;
    }

}

编辑:

我必须手动设置divisionEntity和unitEntity:

@RequestMapping(value = "/addCard", method = RequestMethod.POST)
    public String addCard(HttpServletRequest request, @ModelAttribute(value="card") CardEntity card, BindingResult result)
    {
        card.setDivision(divisionService.findOne(Integer.parseInt(request.getParameter("division"))));
        card.setUnit(unitService.findOne(Integer.parseInt(request.getParameter("unit"))));
        cardService.addCard(card);
        return "redirect:/card";
    }

所以我想问题是divisionEntity并且在提交表单后没有在卡片中设置unitEntity个对象。

1 个答案:

答案 0 :(得分:1)

您也可以这样做:

<tr>
    <td><label for="division.id" class="control-label">Division : </label></td>
    <td><select name="division.id" class="selectpicker">
        <option>Select</option>
        <c:forEach items="${divisions}" var="division">
            <option value="${division.id}">${division.name}</option>
        </c:forEach>
        </select></td>
</tr>
<tr>
    <td><label for="unit.id" class="control-label">Unit : </label></td>
    <td><select name="unit.id" class="selectpicker">
        <option>Select</option>
        <c:forEach items="${units}" var="unit">
            <option value="${unit.id}">${unit.name}</option>
        </c:forEach>
        </select></td>
</tr>

其余部分与原作相同。但是:

  1. 确保您在Division class;
  2. 中有默认的constrcutor
  3. CardEntity应该有getUnit()setUnit(),而不是getUnits()setUnits()
  4. 希望有所帮助。