使用@OneToMany 关系加载 primefaces 拖曳动态 selectOneMenu
Load primefaces tow dynamic selectOneMenu with @OneToMany relation
我想加载两个 primefaces selectOneMenu 但只有一个 select 来自数据库。
Class 国家:
@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;
Class城市
@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>
谁能帮我做这个吗?
是的,拉斐尔罗斯我必须这样做:
@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" />
我想加载两个 primefaces selectOneMenu 但只有一个 select 来自数据库。
Class 国家:
@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;
Class城市
@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>
谁能帮我做这个吗?
是的,拉斐尔罗斯我必须这样做:
@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" />