title: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException
body: <p>This is my orderservice implementation for creating and saving orders here with the customerid I'm getting customer and customer has a cart and in the cart product list is there and for that product list, I should create an order.</p>
  <pre><code>public Order save(int custid) {
          Optional&lt;Customer&gt; cust = customerRepo.findById(custid);//here customer is there and inside customer cart is there inside cart medicine list is there.
          Cart ct= cust.get().getCart();//getting cart from customer
          if(ct.getMedicineList().size()!=0) {//create order only if the medicine list is not empty. there are 8 fields **orderDate,dispatchDate,status,medicineList,totalCost,customer and orderId(GeneratedValue)** I can't set the orderId cuz it is auto generated.
              LocalDate todaysDate = LocalDate.now();
              LocalDate dispatchdate = todaysDate.plusDays(3);
              List&lt;Medicine&gt; orderList= new ArrayList&lt;Medicine&gt;();
              List&lt;Medicine&gt; cartList= new ArrayList&lt;Medicine&gt;();
              cartList=ct.getMedicineList();
              orderList.addAll(cartList);
              Order ord = new Order();
              ord.setCustomer(cust.get());
              ord.setMedicineList(orderList);
              ord.setDispatchDate(dispatchdate);
              ord.setOrderDate(todaysDate);
              ord.setStatus(&quot;Placed&quot;);
              ord.setTotalCost((float)ct.getTotalAmount());
              logger.info(&quot;Add order to the database&quot;);
              return orderRepository.save(ord);
          }
          return null;
          
      }
  </code></pre>
  <p>this is my order controller</p>
  <pre><code>@PostMapping(&quot;/order/{custid}&quot;)
      public ResponseEntity&lt;Order&gt; addOrder(@Valid @PathVariable(&quot;custid&quot;) int custid) {
          logger.info(&quot;Add order in database&quot;);
          return new ResponseEntity&lt;&gt;(orderService.save(custid), HttpStatus.OK);
      }
  </code></pre>
  <p>this is my medicine Entity</p>
  <pre><code>@Data
  @RequiredArgsConstructor
  @ToString
  @Table(name = &quot;medicine&quot;)
  @Entity
  public class Medicine {
      @Id
      @Column(name = &quot;medicine_id&quot;, nullable = false)
      @NonNull
      @GeneratedValue
      private int medicineId;
      
      @NonNull
      @Size(min = 3, message = &quot;Minimum charecters in medicine name should be 3.&quot;)
      @NotEmpty
      @Column(unique = true, name = &quot;medicine_name&quot;, nullable = false)
      
      private String medicineName;
      
      @NonNull
      @Column(name = &quot;medicine_cost&quot;, nullable = false)
      private float medicineCost;
      
      @NonNull
      @Column(name = &quot;mfd&quot;, nullable = false)
      private LocalDate mfd;
      
      @NonNull
      @Column(name = &quot;expiry_date&quot;, nullable = false)
      private LocalDate expiryDate;
      
      @NonNull
      @Column(name = &quot;medicine_quantity&quot;, nullable = false)
      private int medicineQuantity = 1;
      @NonNull
      private String medicineCategory;
      @NonNull
      private String medicineDescription;
  
  
  
      @JsonIgnore
      @ManyToMany(cascade = CascadeType.ALL, mappedBy = &quot;medicineList&quot;,fetch = FetchType.EAGER)
      private List&lt;Order&gt; orderList;
  }
  </code></pre>
  <p>this is my order Entity</p>
  <pre><code>@Data
  @AllArgsConstructor
  @NoArgsConstructor
  @ToString
  
      public class Order {
          @Id
          @Column(name = &quot;orderId&quot;)
          @GeneratedValue
          private int orderId;
          @NonNull
          private LocalDate orderDate;
          @ManyToMany(targetEntity = Medicine.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
          @JoinTable(name = &quot;ord_med&quot;, joinColumns = { @JoinColumn(name = &quot;ord_id&quot;) }, inverseJoinColumns = {
                  @JoinColumn(name = &quot;med_id&quot;) })
          private List&lt;Medicine&gt; medicineList = new ArrayList&lt;&gt;();
          @NonNull
          private LocalDate dispatchDate;
          @NotEmpty
          private float totalCost;
          @NonNull
          private String status;
          
          @ManyToOne(cascade = CascadeType.ALL)
          @JoinColumn(name=&quot;c_ord_fk&quot;,referencedColumnName = &quot;customerId&quot;)
          @NonNull
          private Customer customer;
          
      }
  </code></pre>
  <p>here when i try to create order for the list inside cart it gives me <em>[36m.m.m.a.ExceptionHandlerExceptionResolver[0;39m [2m:[0;39m Resolved [org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction]</em>
  i'm not sure but i think its because of the id but it is autogenerated. Idk how to create an order object for the list of products or is this the correct way to do it.</p>