Jsf 加载带有@OneToMany关系的动态selectOneMenu

Jsf 加载带有@OneToMany关系的动态selectOneMenu,jsf,jpa,primefaces,one-to-many,selectonemenu,Jsf,Jpa,Primefaces,One To Many,Selectonemenu,我想加载两个primefaces selectOneMenu,但只从数据库中选择一个 <p:outputLabel for="city" value="City: " /> <p:selectOneMenu id="city" value="#{countryView.selectedCity}" style="width:150px" converter="#{cityConverter}"> <f:selectItems val

我想加载两个primefaces selectOneMenu,但只从数据库中选择一个

    <p:outputLabel for="city" value="City: " />
    <p:selectOneMenu id="city" value="#{countryView.selectedCity}" style="width:150px"  converter="#{cityConverter}">
        <f:selectItems value="#{countryView.selectedCountry.cities}" itemLabel="#{selectedCity.libelle}" var="selectedCity" itemValue="#{selectedCity}"/>
    </p:selectOneMenu> 
阶级国家:

@Entity
@Table(name = "COUNTRY")
public class Country implements Serializable{


private static final long serialVersionUID = -2160543740997716714L;


@Id
@Column(name = "ID_COUNTRY")
private Long idTCountry;

@Column(name = "LIBELLE_COUNTRY")
private String libelleCountry;


@OneToMany(mappedBy="country")
  private List<City> Cities;
    <p:outputLabel for="city" value="City: " />
    <p:selectOneMenu id="city" value="#{countryView.selectedCity}" style="width:150px"  converter="#{cityConverter}">
        <f:selectItems value="#{countryView.selectedCountry.cities}" itemLabel="#{selectedCity.libelle}" var="selectedCity" itemValue="#{selectedCity}"/>
    </p:selectOneMenu> 
在XHTML中如何动态加载这两个SelectOne菜单

    <p:outputLabel for="city" value="City: " />
    <p:selectOneMenu id="city" value="#{countryView.selectedCity}" style="width:150px"  converter="#{cityConverter}">
        <f:selectItems value="#{countryView.selectedCountry.cities}" itemLabel="#{selectedCity.libelle}" var="selectedCity" itemValue="#{selectedCity}"/>
    </p:selectOneMenu> 
我这样做了,但没有起作用:

        <p:outputLabel for="country" value="Country: " />
        <p:selectOneMenu id="country" value="#{countryView.selectedCountry}"  style="width:150px">
            <p:ajax listener="#{countryView.onTypeChange}" update="city" />
            <f:selectItems value="#{countryView.countries}" itemLabel="#{selectedType.libelleCountry}" var="selectedCountry" />
        </p:selectOneMenu>

        <p:outputLabel for="city" value="City: " />
        <p:selectOneMenu id="city" value="#{countryView.selectedCity}" style="width:150px">
            <f:selectItems value="#{countryView.selectedCountry.cities}" itemLabel="#{selectedCity.libelle}" var="selectedCity" itemValue="#{selectedCity}"/>
        </p:selectOneMenu> 
    <p:outputLabel for="city" value="City: " />
    <p:selectOneMenu id="city" value="#{countryView.selectedCity}" style="width:150px"  converter="#{cityConverter}">
        <f:selectItems value="#{countryView.selectedCountry.cities}" itemLabel="#{selectedCity.libelle}" var="selectedCity" itemValue="#{selectedCity}"/>
    </p:selectOneMenu> 


有谁能帮我做这件事吗?

是的,拉斐尔·罗斯我必须做这件事:

@OneToMany(fetch = FetchType.EAGER, mappedBy="country", cascade =    CascadeType.ALL)
private List<City> cities;
    <p:outputLabel for="city" value="City: " />
    <p:selectOneMenu id="city" value="#{countryView.selectedCity}" style="width:150px"  converter="#{cityConverter}">
        <f:selectItems value="#{countryView.selectedCountry.cities}" itemLabel="#{selectedCity.libelle}" var="selectedCity" itemValue="#{selectedCity}"/>
    </p:selectOneMenu> 
@OneToMany(fetch=FetchType.EAGER,mappedBy=“country”,cascade=CascadeType.ALL)
私人名单城市;
当然,我还必须添加转换器:

    <p:outputLabel for="city" value="City: " />
    <p:selectOneMenu id="city" value="#{countryView.selectedCity}" style="width:150px"  converter="#{cityConverter}">
        <f:selectItems value="#{countryView.selectedCountry.cities}" itemLabel="#{selectedCity.libelle}" var="selectedCity" itemValue="#{selectedCity}"/>
    </p:selectOneMenu> 

正如您所见,在ajax事件中我不需要侦听器:

    <p:outputLabel for="city" value="City: " />
    <p:selectOneMenu id="city" value="#{countryView.selectedCity}" style="width:150px"  converter="#{cityConverter}">
        <f:selectItems value="#{countryView.selectedCountry.cities}" itemLabel="#{selectedCity.libelle}" var="selectedCity" itemValue="#{selectedCity}"/>
    </p:selectOneMenu> 
 <p:ajax update="city" />

是的,拉斐尔·罗斯我必须这样做:

@OneToMany(fetch = FetchType.EAGER, mappedBy="country", cascade =    CascadeType.ALL)
private List<City> cities;
    <p:outputLabel for="city" value="City: " />
    <p:selectOneMenu id="city" value="#{countryView.selectedCity}" style="width:150px"  converter="#{cityConverter}">
        <f:selectItems value="#{countryView.selectedCountry.cities}" itemLabel="#{selectedCity.libelle}" var="selectedCity" itemValue="#{selectedCity}"/>
    </p:selectOneMenu> 
@OneToMany(fetch=FetchType.EAGER,mappedBy=“country”,cascade=CascadeType.ALL)
私人名单城市;
当然,我还必须添加转换器:

    <p:outputLabel for="city" value="City: " />
    <p:selectOneMenu id="city" value="#{countryView.selectedCity}" style="width:150px"  converter="#{cityConverter}">
        <f:selectItems value="#{countryView.selectedCountry.cities}" itemLabel="#{selectedCity.libelle}" var="selectedCity" itemValue="#{selectedCity}"/>
    </p:selectOneMenu> 

正如您所见,在ajax事件中我不需要侦听器:

    <p:outputLabel for="city" value="City: " />
    <p:selectOneMenu id="city" value="#{countryView.selectedCity}" style="width:150px"  converter="#{cityConverter}">
        <f:selectItems value="#{countryView.selectedCountry.cities}" itemLabel="#{selectedCity.libelle}" var="selectedCity" itemValue="#{selectedCity}"/>
    </p:selectOneMenu> 
 <p:ajax update="city" />


在java中,所有对象都已正确加载,在xhtml中,国家列表(selectOneMenu)已加载,但城市列表始终为空。我认为我应该在XHTML中修改selectOneMenu city的属性,但我不知道如何修改。对于Primefaces部分,只需从Primefaces showcase中获取代码(如果您还没有这样做的话):。对于JPA部分,或者您确定cities已初始化(默认情况下,集合是延迟加载的,至少在hibernate中是这样)为了进一步帮助您,我们需要在showcase中使用您的支持bean代码(countryView)。第二个列表由Ajax事件加载,而不是自动加载:。onCountryChange():cities=data.get(国家);在JPA中,一切正常。当我检查一个国家对象时,它包含一个城市对象列表。Si cities在java中加载所有对象均已正确加载,在xhtml中已加载国家列表(selectOneMenu),但城市列表始终为空。我认为我应该在XHTML中修改selectOneMenu city的属性,但我不知道如何修改。对于Primefaces部分,只需从Primefaces showcase中获取代码(如果您还没有这样做的话):。对于JPA部分,或者您确定cities已初始化(默认情况下,集合是延迟加载的,至少在hibernate中是这样)为了进一步帮助您,我们需要在showcase中使用您的支持bean代码(countryView)。第二个列表由Ajax事件加载,而不是自动加载:。onCountryChange():cities=data.get(国家);在JPA中,一切正常。当我检查一个国家对象时,它包含一个城市对象列表。Si cities已加载