简体   繁体   中英

Implementing one to one relation ship spring hibernate

I have a table cards which is related to some other master tables (divisions and units) with one to one relation.

On UI I am showing drop downs to select the division and unit values for card, this is the function to create form in 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>

Drop downs are populated with the data but on submitting form its not setting division or unit to the cardEntity object they are set to null, this is the addCard function in controller :

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

There are other fields which are added in the card except drop downs.

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

}

Edit :

I have to set divisionEntity and unitEntity manually :

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

So I guess the problem is divisionEntity and unitEntity objects are not set in card after submitting the form.

You can do this too:

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

The rest is same as your original. But:

  1. Make sure you have a default constrcutor in Division class;
  2. The CardEntity should have getUnit() and setUnit() , not getUnits() and setUnits() .

Hope it help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM