【问题标题】:Spring MVC + Hibernate Cannot get item propertySpring MVC + Hibernate 无法获取项目属性
【发布时间】: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代码,但我不知道如何让它正确。需要帮助,请!

【问题讨论】:

    标签: spring hibernate


    【解决方案1】:

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

    【讨论】:

    • ava.lang.IllegalArgumentException: 'items' 不能为空 org.springframework.util.Assert.notNull(Assert.java:115) org.springframework.web.servlet.tags.form.AbstractMultiCheckedElementTag .setItems(AbstractMultiCheckedElementTag.java:84) org.apache.jsp.WEB_002dINF.jsp.sales.saleform_jsp._jspx_meth_form_005fradiobuttons_005f0(saleform_jsp.java:571) org.apache.jsp.WEB_002dINF.jsp.sales.saleform_jsp.javajspService(saleform_jsp. :265) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 我需要访问 OrderItem 并且可以t reach this field when im 尝试添加它。
    【解决方案2】:

    解决了。我在项目中重新制作了一些东西,在 jsp 部分。添加附加页面,一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-09
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      • 2017-10-13
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      相关资源
      最近更新 更多