0

I have two tables one is merchant other one is product. First i will add merchant details then i have to select merchants and assign those merchant to product. Problem is when I assign merchant details with id It throwing an error.


    {
            "name": "product A",
            "quantityType": "kg",
            "status": false,
            "quantity": "30",
            "createdBy": null,
            "modifiedBy": null,
            "createdDate": null,
            "modifiedDate": null,
            "merchantSet": [
                {
                            "id":4,
                            "idNo": "XXXXXXXXXXV",
                    "idType": "NIC",
                    "emailId": "majutharan01@gmail.com",
                    "name": "aaaaaaaaa",
                    "contactNo": "0766601122",
                    "status": true,
                    "createdBy": null,
                    "modifiedBy": null,
                    "createdDate": null,
                    "modifiedDate": null
                }
            ],
            "category": {
                "id": 1
            }
        } 

This is the error:


    {
        "timestamp": 1554450979205,
        "status": 500,
        "error": "Internal Server Error",
        "message": "detached entity passed to persist: com.xxxxx.xxxxxxx.model.Merchant; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.digiratina.islandgreen.model.Merchant",
        "path": "/product-service/save-product"
    }

product table

Merchant table

Intermediate Table

when I add merchant details without id it will save but it creating new id to merchant and updated intermediate table but my scenario I have to add existing merchant id to intermediate table.

Merchant Table...


    @Getter
    @Setter
    @Entity
    @Table(name = "merchant")
    public class Merchant {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY) // Auto generate Id...
        @Column(name = "id")
        private Long id;

        @Column(name = "id_no")
        private String idNo;

        @Column(name = "id_type")
        private String idType;

        @Column(name = "email_id")
        private String emailId;

        @Column(name = "name")
        private String name;

        @Column(name = "contact_no")
        private String contactNo;

        @Column(name = "status")
        private boolean status;

        @Column(name = "created_by")
        private String createdBy;

        @Column(name = "modified_by")
        private String modifiedBy;

        @Column(name = "created_date")
        private Date createdDate;

        @Column(name = "modifiedDate")
        private Date modifiedDate;

        @ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL}, mappedBy="merchantSet")
        @Getter(onMethod = @__( @JsonIgnore))
        @Setter
        private Set<Product> productSet = new HashSet<Product>();

product Table...


     @Getter
    @Setter
    @Entity
    @Table(name = "product")
    public class Product {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY) // Auto generate Id...
        @Column(name = "id")
        private Long id;

        @Column(name = "name")
        private String name;

        @Column(name = "quantity_type")
        private String quantityType;

        @Column(name = "status")
        private boolean status;

        @Column(name = "quantity")
        private String quantity;

        @Column(name = "created_by")
        private String createdBy;

        @Column(name = "modified_by")
        private String modifiedBy;

        @Column(name = "created_date")
        private Date createdDate;

        @Column(name = "modifiedDate")
        private Date modifiedDate;

    //    @ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL}, mappedBy="productSet")
            @ManyToMany(cascade = {CascadeType.ALL, CascadeType.PERSIST, CascadeType.MERGE})
        @JoinTable(name = "product_merchant",
                joinColumns = {@JoinColumn(name = "product_id")},
                inverseJoinColumns = {@JoinColumn(name = "merchant_id")})
        private Set<Merchant> merchantSet = new HashSet<Merchant>();

        @ManyToOne(cascade = {
                CascadeType.PERSIST,
                CascadeType.MERGE
        })
        @JoinColumn(name = "category_id", nullable = false)
        @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
        private Category category;

        @OneToMany(fetch = FetchType.LAZY, mappedBy = "product")
        @Getter(onMethod = @__( @JsonIgnore ))
        @Setter
        private Set<Batch> batches = new HashSet<Batch>(
                0);

        public Product() {
        }

        public Product(Long id, String name, String quantityType, boolean status, String quantity, String createdBy, String modifiedBy, Date createdDate, Date modifiedDate) {
            this.id = id;
            this.name = name;
            this.quantityType = quantityType;
            this.status = status;
            this.quantity = quantity;
            this.createdBy = createdBy;
            this.modifiedBy = modifiedBy;
            this.createdDate = createdDate;
            this.modifiedDate = modifiedDate;
        }

        public Product(String name, String quantityType, boolean status, String quantity, String createdBy, String modifiedBy, Date createdDate, Date modifiedDate, Set<Merchant> merchantSet, Category category) {
            this.name = name;
            this.quantityType = quantityType;
            this.status = status;
            this.quantity = quantity;
            this.createdBy = createdBy;
            this.modifiedBy = modifiedBy;
            this.createdDate = createdDate;
            this.modifiedDate = modifiedDate;
            this.merchantSet = merchantSet;
            this.category = category;
        }
    } ```


    This is my service...

    ``` public Product addProduct(Product product) {
            if (product.getCategory().getId() != null && product.getMerchantSet() != null && product.getCategory().getId() > 0) {
                product.setCategory(categoryRepository.findCategoryById(product.getCategory().getId()));
                product.setMerchantSet(product.getMerchantSet());
                Product saveProduct = productRepository.save(product);
                return saveProduct;
            }
            return null;
        } 

0 Answers0