开发者

Self-referenced entity

I have an entity called person. Each person has three fields :

sexe (male, female) mother (self referenced to person) father (self referenced to person)

Now, in my view jspx, I would like the two selected fields to only display female personn in the mother select and male personns in the fath开发者_JS百科er field. What would be the best way to do that ?


First of all, you should create dynamic finder to get persons by sex. Type in Roo console:

finder list

Then find something like findPeopleBySex and type:

finder add --finderName findPeopleBySex

If you don't want to scaffold this finder you can add exposeFinders = false to the @RooWebScaffold annotation of your PersonController.

Then you should open your PersonController (which body is empty I suppose) and place there something like the following:

@ModelAttribute("men")
public Collection<Person> populateMen() {
    return Person.findPeopleBySex(Gender.Male).getResultList();
}

@ModelAttribute("women")
public Collection<Person> populateWomen() {
    return Person.findPeopleBySex(Gender.Female).getResultList();
}

Finally open your {project_root}/src/main/webapp/WEB-INF/views/people/create.jspx then find the following lines:

    <field:select field="mother" id="c_xxx_Person_mother" itemValue="id" items="${people}" path="/people" z="xxx"/>
    <field:select field="father" id="c_xxx_Person_father" itemValue="id" items="${people}" path="/people" z="xxx"/>

And change them to be:

    <field:select field="mother" id="c_xxx_Person_mother" itemValue="id" items="${women}" path="/people" z="xxx"/>
    <field:select field="father" id="c_xxx_Person_father" itemValue="id" items="${men}" path="/people" z="xxx"/>

Make sure the value of z attribute (hash code) become user-managed. It means Roo will not change it in future.

Now you can run your application and see the result.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜