【问题标题】:Load primefaces tow dynamic selectOneMenu with @OneToMany relation使用 @OneToMany 关系加载 primefaces 动态 selectOneMenu
【发布时间】:2016-03-17 16:21:50
【问题描述】:

我想加载两个primefaces 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;

类城市

@Entity
@Table(name = "CITY")
public class City implements Serializable{

private static final long serialVersionUID = -1617401771255693322L;

@Id
@Column(name = "ID_CITY")
private Long idCity;

@ManyToOne
@JoinColumn(name="COUNTRY")
private Country country;

@Column(name = "LIBELLE")
private String libelle;

如何在 XHTML 中动态加载这些拖拽 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> 

谁能帮我做这件事吗?

【问题讨论】:

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

标签: jsf jpa primefaces one-to-many selectonemenu


【解决方案1】:

是的 Raphael Roth 我必须这样做:

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

如你所见,我在 ajax 事件中不需要监听器:

 <p:ajax update="city" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    相关资源
    最近更新 更多