1

Developing an application of shopping cart below are my entity classes....

When i save it for the first time user entity is saved properly when same request is used to save user again then previously saved foreign key is becoming null i am using same request because, for same user, for same cart ,cart products have to updated in multiple rows.

    @Entity
    public class CartProduct implements Serializable {

            /**
             * serialVersionUID.
             */
            private static final long serialVersionUID = 5846027470952949766L;

            /**
             * cartProdcutId.
             */
            @Id
            @GeneratedValue
            @Column(name = "CART_PRODUCT_ID")
            private Integer cartProdcutId;

            /**
             * product.
             */
            @ManyToOne
            @JoinColumn(name = "PRODUCT_ID")
            private Product product;

            /**
             * cart.
             */
            @ManyToOne
            @JoinColumn(name = "CART_ID")
            private Cart cart;

            /**
             * quantity.
             */
            @Min(value = 0, message = "Product Quantity should not be negative")
            @Column(name = "QUANTITY")
            private Integer quantity;

}

second class

    @Entity
    public class Cart {
        /**
         * cartId.
         */
        @Id
        @GeneratedValue
        @Column(name = "CART_ID")
        Integer cartId;
        /**
         * cartProducts.
         */
        @OneToMany(cascade = CascadeType.ALL)
        @JoinColumn(name = "CART_ID")
        Set<CartProduct> cartProducts = new HashSet<CartProduct>();
 }

Saving User class

    @Entity
    public class User {

        /**
         * userId.
         */
        @Id
        @GeneratedValue
        @Column(name = "USER_ID")
        Integer userId;
        /**
         * userName.
         */
        @Column(name = "USER_NAME")
        String userName;
        /**
         * cart.
         */
        @OneToOne(cascade = CascadeType.ALL)
        Cart cart;
}    

Product Class

    @Entity
    @Inheritance(strategy = InheritanceType.JOINED)
    @NamedQueries({ @NamedQuery(name = "Product.findBook", query = "SELECT p FROM Product p WHERE TYPE(p) = Book"),
            @NamedQuery(name = "Product.findApparal", query = "SELECT p FROM Product p WHERE TYPE(p) = Apparal"),
            @NamedQuery(name = "Product.findByName", query = "SELECT p FROM Product p WHERE p.productName=:name") })
    public class Product {

        /**
         * productId.
         */
        @Id
        @GeneratedValue
        @Column(name = "PRODUCT_ID")
        Integer productId;
        /**
         * productName.
         */
        @Column(name = "PRODUCT_NAME")
        String productName;
        /**
         * price.
         */
        @Column(name = "PRICE")
        Float price;
 }

Service method to save User

userRepository.save(user);

Json used in postman to save User entity:

{
  "cart": {
    "cartId": 1,
    "products": [
      {
        "cartProdcutId": 1,
        "product": {
          "price": 100,
          "productId": 1,
          "productName": "ProdNameOne"
        },
        "quantity": 1
      }
    ]
  },
  "userId": 1,
  "userName": "USERONE"
}

Below is the data base screen shot where null is updated in first row :Data base screen shot

R.G
  • 6,436
  • 3
  • 19
  • 28
Darshan
  • 11
  • 1
  • When you `update` , are you first fetching the entity from the db , modifying the entity instance as required and then updating ? – R.G Apr 11 '20 at 04:31
  • Hi R.G. Thanks for the reply. I am not doing that. Directly calling save method on entity. Do I have to fetch, update and then save it?? Why? Can u explain please. – Darshan Apr 12 '20 at 06:40
  • An update is done to a record that already exist. If an update is attempted with a partially populated entity instance , that will be the final state of that instance. When the entity is fetched from the repository , you get the entity with the latest state. An update done to the latest state ( which is not partial ) and saved should ideally solve your issue – R.G Apr 12 '20 at 06:46
  • 1
    Hi R.G. Thanks for the solution. I fetched the user from the DB and updated the values and saved it again. It worked. – Darshan Apr 12 '20 at 08:52

0 Answers0