【发布时间】:2016-06-04 10:10:24
【问题描述】:
我,卡住了。我的数据输出有问题。我尝试制作某种订单产品项目。我的实体如下:
@Entity
@Table(name = "sales")
public class Sale implements Serializable {
public Sale() {
}
@Id
@Column
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(insertable = false, updatable = false)
private Timestamp date;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "sale")
private List<OrderItem> items = new ArrayList<OrderItem>();
@Column
private double cost;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Timestamp getDate() {
return date;
}
public void setDate(Timestamp date) {
this.date = date;
}
public List<OrderItem> getItems() {
return items;
}
public void setItems(List<OrderItem> items) {
this.items = items;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
for(OrderItem item : items)
cost += item.getProduct().getPrice() * item.getQuantity();
this.cost = cost;
}
}
@Entity
@Table(name = "products")
public class Product implements Serializable {
public Product() {
}
@Id
@Column
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column
private String name;
@Column
private double price;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "product")
private Set<OrderItem> items = new HashSet<OrderItem>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Set<OrderItem> getItems() {
return items;
}
public void setItems(Set<OrderItem> items) {
this.items = items;
}
public boolean isNew() {
return this.id == 0;
}
}
@Entity
@Table(name = "order_items")
public class OrderItem implements Serializable {
@Id
@GeneratedValue
@Column(name = "id")
private Long id;
@Column
private int quantity;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "product_id")
private Product product;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "sale_id")
private Sale sale;
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
}
SQL 表的创建方式如下: 创建表产品( id 序列主键不为空, 名称 CHARACTER(50) NOT NULL, 价格 REAL NOT NULL ) WITH ( OIDS = FALSE );
创建表销售 ( id 序列主键不为空, cost REAL NOT NULL, date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ) WITH ( OIDS = FALSE );
创建表 order_items ( id SERIAL NOT NULL, sale_id 整数非空, product_id 整数, 数量 INTEGER NOT NULL, 主键(sale_id,id) ) WITH ( OIDS = FALSE );
ALTER TABLE order_items 添加约束 order_itemsFK0 FOREIGN KEY (product_id) REFERENCES products(id); 更改表 order_items 添加约束 order_itemsFK1 FOREIGN KEY (sale_id) REFERENCES sales(id);
我的销售表格:
<form:hidden path="id" />
<spring:bind path="items">
<div class="form-group ${status.error ? 'has-error' : ''}">
<label class="col-sm-2 control-label">Product</label>
<div class="col-sm-5">
<form:select path="items" class="form-control">
<form:options items="${productMap}" />
</form:select>
<form:errors path="items" class="control-label" />
</div>
<div class="col-sm-5"></div>
</div>
</spring:bind>
<spring:bind path="items">
<div class="form-group ${status.error ? 'has-error' : ''}">
<label class="col-sm-2 control-label">Quantity</label>
<div class="col-sm-10">
<form:radiobuttons path="items" items="${numberList}" element="label class='radio-inline'" />
<br />
<form:errors path="items" class="control-label" />
</div>
</div>
</spring:bind>
<spring:bind path="cost">
<div class="form-group ${status.error ? 'has-error' : ''}">
<label class="col-sm-2 control-label">Cost</label>
<div class="col-sm-10">
<form:input path="cost" type="text" class="form-control" id="cost"
placeholder="Cost" />
<form:errors path="cost" class="control-label" />
</div>
</div>
</spring:bind>
我在尝试添加销售的表单上遇到问题。项目不正确,不保存。我写错了jsp代码,但我不知道如何让它正确。需要帮助,请!
【问题讨论】: