2015-03-19 67 views
0

我有一個表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對象時,它們被設置爲空,這是控制器中的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對象不卡設置提交表單後。

+0

和你的問題是? – mithrandir 2015-03-19 09:56:42

+0

如果我從下拉菜單中選擇一個分區或單元並提交表單,它應該將card_id和unit_of_qty_id保存在卡表中,但它們是空的。 – Naveen 2015-03-19 10:06:01

回答

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類中有默認constrcutor;
  2. CardEntity應該有getUnit()setUnit(),而不是getUnits()setUnits()

希望它有幫助。

+0

這是我對'setUnits()'和'getUnits()'的拼寫錯誤,從'」已經工作。謝謝! – Naveen 2015-03-21 09:54:53