首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring + Hibernate无法获得项属性

问Spring + Hibernate无法获得项属性
EN

Stack Overflow用户
提问于 2016-02-22 20:10:07
回答 2查看 94关注 0票数 0

我被卡住了。我对数据的输出有问题。我试着做一些订单产品项目。我的实体如下:

代码语言:javascript
复制
  @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系列主键NULL,名称字符(50) NULL,价格真实NULL )( OIDS = FALSE );

创建表销售( id系列主键NULL,成本实际NULL,日期时间戳NULL CURRENT_TIMESTAMP )( OIDS = FALSE );

创建表order_items ( id串行NULL,sale_id INTEGER,product_id整数,数量整数NULL,主键(sale_id,id) )( OIDS = FALSE );

ALTER order_items添加约束order_itemsFK0外键(product_id)引用产品(Id);ALTER order_items添加约束order_itemsFK1外键(sale_id)引用销售(Id);

我的销售表格:

代码语言:javascript
复制
    <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代码,但我不知道如何把它做对。需要帮助求你了!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-03 14:52:53

解决了。我在这个项目中重新制作了一些东西,在jsp部分。添加添加页面,一切都很好。

票数 0
EN

Stack Overflow用户

发布于 2016-02-22 20:34:37

你有原木吗?确保您的帖子到达服务器端。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35562866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档