2016-02-22 26 views
0

I,M卡住。我有一個輸出數據的問題。我嘗試做一些訂單產品項目。我的實體是以下幾點:的Spring MVC + Hibernate無法獲取項目屬性

@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表創建這樣的: CREATE TABLE產品( ID SERIAL PRIMARY KEY NOT NULL, 名字符(50)NOT NULL, 價格REAL NOT NULL ) WITH( OIDS = FALSE);

CREATE TABLE銷售( ID的序列PRIMARY KEY NOT NULL, 成本REAL NOT NULL, 日期TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ) WITH(OIDS = FALSE);

CREATE TABLE ORDER_ITEMS( ID的序列NOT NULL, sale_id INTEGER NOT NULL, PRODUCT_ID INTEGER, 量INTEGER NOT NULL, 主鍵(sale_id,ID) )WITH(OIDS = FALSE) ;

ALTER TABLE order_items ADD CONSTRAINT order_itemsFK0 FOREIGN KEY(product_id)REFERENCES products(id); ALTER TABLE ORDER_ITEMS ADD CONSTRAINT order_itemsFK1外鍵(sale_id)參考文獻銷售(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代碼,但我不知道如何得到它的權利。需要幫助,請!

回答

0

解決。我在jsp部分重新編寫了項目中的一些東西。添加添加劑頁面,一切正常。

0

你有一些日誌?確保你的帖子到達服務器端。

+0

ava.lang.IllegalArgumentException: '項目' 不能爲空 \t org.springframework.util.Assert.notNull(Assert.java:115) \t org.springframework.web.servlet.tags.form.AbstractMultiCheckedElementTag。 setItems(AbstractMultiCheckedElementTag.java:84) \t org.apache.jsp.WEB_002dINF.jsp.sales.saleform_jsp._jspx_meth_form_005fradiobuttons_005f0(saleform_jsp.java:571) \t org.apache.jsp.WEB_002dINF.jsp.sales.saleform_jsp._jspService( saleform_jsp.java:265) \t org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)我需要OrderItem的存取,並不能老是達到這一領域時,我真的嘗試添加它。 – mzherdev