实现一对一关系 spring hibernate

Implementing one to one relation ship spring hibernate

我有一个 table cards 与其他一些主 tables(部门和单位)具有一对一的关系。

在 UI 我正在显示 select 卡的分区和单位值的下拉菜单,这是在 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";
    }

所以我猜问题是 divisionEntityunitEntity 对象在提交表单后没有设置在卡片中。

你也可以这样做:

<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. 中有默认构造函数
  3. CardEntity应该有getUnit()setUnit(),而不是getUnits()setUnits()

希望对您有所帮助。