当我从p:selectOneMenu
选择“新价格”时,我收到验证错误“值无效”。当我在bugBean.generateNewPrice()
手动设置新价格的ID时,这会停止
即使是自动生成的更有意义的id值也不是一个好主意,因为在将记录合并/持久化到数据库之前不应设置id值,以确保它没有被占用。
bug.xhtml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<h:form id="selectTest">
<p:selectOneMenu id="selectPrice" value="#{bugBean.selectedPrice}" converter="omnifaces.SelectItemsConverter">
<f:selectItem itemValue="" itemLabel="choose price"/>
<f:selectItem itemValue="#{bugBean.generateNewPrice()}" itemLabel="new price"/>
<f:selectItems value="#{bugBean.getPrices()}" var="price" itemLabel="#{price.value}"/>
<p:ajax update="@all"/>
</p:selectOneMenu>
<p:messages autoUpdate="true"/>
</h:form>
</h:body>
</html>
BugBean.java
package hoho.main.managebean;
import java.io.Serializable;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import hoho.model.generated.Price;
import hoho.service.BugService;
@Named
@SessionScoped
public class BugBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Inject
BugService bugService;
private Price selectedPrice;
public List<Price> getPrices(){
List<Price> prices = bugService.getPrices();
return prices;
}
public Price generateNewPrice(){
Price price = new Price();
// price.setId(424234234L);
return price;
}
public Price getSelectedPrice() {
return selectedPrice;
}
public void setSelectedPrice(Price selectedPrice) {
this.selectedPrice = selectedPrice;
}
}
价格实体:
package hoho.model.generated;
import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;
import java.util.List;
/**
* The persistent class for the price database table.
*
*/
@Entity
public class Price implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private BigDecimal value;
//bi-directional many-to-one association to DeliveryMethodHasPrice
@OneToMany(mappedBy="price", fetch=FetchType.EAGER)
private List<DeliveryMethodHasPrice> deliveryMethodHasPrices;
//bi-directional many-to-one association to Item
@OneToMany(mappedBy="price", fetch=FetchType.EAGER)
private List<Item> items;
//bi-directional many-to-one association to TaxRate
@ManyToOne
@JoinColumn(name="tax_rate_id1")
private TaxRate taxRate;
public Price() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public BigDecimal getValue() {
return this.value;
}
public void setValue(BigDecimal value) {
this.value = value;
}
public List<DeliveryMethodHasPrice> getDeliveryMethodHasPrices() {
return this.deliveryMethodHasPrices;
}
public void setDeliveryMethodHasPrices(List<DeliveryMethodHasPrice> deliveryMethodHasPrices) {
this.deliveryMethodHasPrices = deliveryMethodHasPrices;
}
public List<Item> getItems() {
return this.items;
}
public void setItems(List<Item> items) {
this.items = items;
}
public TaxRate getTaxRate() {
return this.taxRate;
}
public void setTaxRate(TaxRate taxRate) {
this.taxRate = taxRate;
}
@Override
public boolean equals(Object other) {
System.out.println("hey");
return (other instanceof Price) && (id != null)
? id.equals(((Price) other).id)
: (other == this);
}
@Override
public int hashCode() {
return (id != null)
? (this.getClass().hashCode() + id.hashCode())
: super.hashCode();
}
@Override
public String toString() {
return "Price[id=" + id + "]";
}
}
答案 0 :(得分:1)
没有办法实现你想要的。我想你需要改变一下你的设计。
New price
作为h:selectOneMenu
的选项,而是将其设为独立h:booleanCheckbox
。当用户勾选该框时,您可以创建一个没有ID的新实体。New price
设为默认选项,而不是Choose one...
。如果用户未选择任何价格,请创建新的Price
实体。