mzherdev
mzherdev

Reputation: 462

Spring MVC + Hibernate Cannot get item property

I,m stuck. I have a problem with output of data. I try to make some kind of order-product project. My Entities are following:

  @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 tables create like this: CREATE TABLE products ( id SERIAL PRIMARY KEY NOT NULL, name CHARACTER(50) NOT NULL, price REAL NOT NULL ) WITH ( OIDS = FALSE );

CREATE TABLE sales ( 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, quantity INTEGER NOT NULL, primary key (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 FOREIGN KEY (sale_id) REFERENCES sales(id);

My sale form:

    <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>

And i have problems on form where I try to add sale. Items is incorrect, doesn`t save. i write jsp code wrong but i have no idea how to get it right. Need help, please!

Upvotes: 0

Views: 98

Answers (2)

mzherdev
mzherdev

Reputation: 462

Solved. I remade some stuff in the project, in jsp part. Add additive page and everything works good.

Upvotes: 0

Do you have some log? make sure that your post is arriving to the server side.

Upvotes: 0

Related Questions