Spring MVC + Hibernate无法获取item属性

时间:2016-02-22 20:10:08

标签: spring 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,   name CHARACTER(50)NOT NULL,   价格REAL NOT NULL   )   WITH(OIDS = FALSE);

CREATE TABLE销售(   id SERIAL PRIMARY KEY NOT NULL,   cost REAL NOT NULL,   date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP   )   WITH(OIDS = FALSE);

CREATE TABLE order_items(     id SERIAL 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     添加约束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代码,但我不知道如何正确。需要帮助!

2 个答案:

答案 0 :(得分:0)

你有一些日志吗?确保你的帖子到达服务器端。

答案 1 :(得分:0)

解决。我在jsp部分重新编写了项目中的一些东西。添加添加页面,一切都很好。

相关问题