Spring对象里面的对象映射hibernate

时间:2017-03-16 08:25:59

标签: java spring hibernate

我正在开发购物车 我将订单pojo保存为

 @Entity
@Table(name="order1")
public class Order1 {


    @Id
    @Column(name="orderid")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int orderid;

    @OneToMany
    private List<KartItem> items;

    enter code here
    private int userid;
    private double grandtotal;
    private String deliveryaddress;
    private String status;
    private Date orderdate;
``//getteres setters

KartItem是我保存项目相关信息的价格,数量,总数等。

 @Entity
@Table(name="kartitem")
    public class KartItem {
    @Id
    @Column(name="kartitemid")  
    @GeneratedValue(strategy=GenerationType.IDENTITY)
        private int KartItemid;
        private int quantity;
        private double total;
        @OneToOne(cascade = CascadeType.ALL)
        private Item item;



        public KartItem() {

        }

        public KartItem( int quantity, double total, Item item) {
            super();

            this.quantity = quantity;
            this.total = total;
            this.item = item;
        }
//getters and setters

而物品pojo是

 @Entity
    @Table(name="item")
    public class Item {

        @Id
        @Column(name="itemid")
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private int itemid;
        private double price;
        private String name;
        private String imagepath;
        private String type;

        //private static final String path="/images/";

        @ManyToOne(cascade=CascadeType.ALL, targetEntity=Product.class)
        @JoinColumn(name="productid", referencedColumnName="productid")
        private Product product;``
//getters and setters

我正在保存Order对象,KartItem对象会自动保存 但是当我尝试保存由相同类别的KartItem组成的订单,即2个电视或2个手机时,它会给出错误

org.hibernate.NonUniqueObjectException:具有相同标识符值的另一个对象已与会话关联:[com.spring.model.Category#21]

0 个答案:

没有答案