0

When I choose "new price" from the p:selectOneMenu I get a validation error "value is not valid". This stops when I set an id for the new price in bugBean.generateNewPrice() manually.
Even an automatically generated id-value that makes more sense wouldn't be a good idea, as the id-value shouldn't be set before merging/persisting the record into the database to make sure it has not been occupied inbetween.

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;
    }   

}

The Price Entity:

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 + "]";
    }

}
4

1 回答 1

1

没有办法达到你想要的。我认为你需要稍微改变你的设计。

  1. 与其将New price其作为 的选项,不如h:selectOneMenu使其成为独立的h:booleanCheckbox. 当用户勾选该框时,您可以创建一个没有 ID 的新实体。
  2. 另一种方法是New price使用默认选项而不是Choose one.... 如果用户没有选择任何价格,则创建一个新Price实体。
于 2013-04-23T12:22:13.487 回答