1

I am developing shopping cart Iam saving order pojo as

 @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 is the pojo where I saving item related information i.e. price, quantity, total etc.

 @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

whereas Item pojo is

 @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

I am saving Order object and KartItem object is getting saved automatically but when I try saving order consisting of KartItems of same category i.e. 2 tv or 2 mobiles then its giving an error as

org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [com.spring.model.Category#21]

0 Answers0