gpt4 book ai didi

java - 如何在持久化之前不设置 Id 的情况下使用新的实体对象?

转载 作者:行者123 更新时间:2023-11-29 13:54:04 25 4
gpt4 key购买 nike

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

}

最佳答案

没有办法实现你想要的。我认为你需要稍微改变一下你的设计。

  1. 不要将新价格作为h:selectOneMenu的选项,而是将其设为独立的h:booleanCheckbox。当用户勾选该框时,您可以创建一个没有 ID 的新实体。
  2. 另一种方法是将新价格设置为默认选项,而不是选择一个...。如果用户未选择任何价格,请创建一个新的 Price 实体。

关于java - 如何在持久化之前不设置 Id 的情况下使用新的实体对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16167868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com