最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

spring boot - Why hibernate is calling update for parent entity or for child entity itself after inserting a child entity in uni

programmeradmin1浏览0评论

Inventory.java:

@Entity
@Table(name = "inventory")
public class Inventory {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "inventory_id")
    private int inverntoryId;
    
    @Column(name = "product_id")
    private int productId;

    @Column(name = "change_type")
    private String changeType;
    
    @Column(name = "quantity")
    private int quantity;
    
    @Column(name = "reason")
    private String reason;
    
    @Column(name = "status")
    private String status;
    
    @Column(name = "status_change_datetime")
    private LocalDateTime statusChangeDatetime;
    
    @Column(name = "adjusted_by")
    private String adjustedBy;
    
    public Inventory() {
        
    }

    public Inventory(int productId, ChangeType changeType, int quantity, String reason, 
            InventoryStatus status, LocalDateTime statusChangeDatetime, String adjustedBy) {
        this.productId = productId;
        this.changeType = changeType.name();
        this.quantity = quantity;
        this.reason = reason;
        this.status = status.name();
        this.statusChangeDatetime = statusChangeDatetime;
        this.adjustedBy = adjustedBy;
    }
    
    public int getInverntoryId() {
        return inverntoryId;
    }

    public void setInverntoryId(int inverntoryId) {
        this.inverntoryId = inverntoryId;
    }

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public ChangeType getChangeType() {
        return ChangeType.valueOf(changeType);
    }

    public void setChangeType(ChangeType changeType) {
        this.changeType = changeType.name();
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public InventoryStatus getStatus() {
        return InventoryStatus.valueOf(status);
    }

    public void setStatus(InventoryStatus status) {
        this.status = status.name();
    }

    public LocalDateTime getStatusChangeDatetime() {
        return statusChangeDatetime;
    }

    public void setStatusChangeDatetime(LocalDateTime statusChangeDatetime) {
        this.statusChangeDatetime = statusChangeDatetime;
    }

    public String getAdjustedBy() {
        return adjustedBy;
    }

    public void setAdjustedBy(String adjustedBy) {
        this.adjustedBy = adjustedBy;
    }

    @Override
    public String toString() {
        return "Inventory [inverntoryId=" + inverntoryId + ", productId=" + productId + ", changeType=" + changeType
                + ", quantity=" + quantity + ", reason=" + reason + ", status=" + status + ", statusChangeDatetime="
                + statusChangeDatetime + ", adjustedBy=" + adjustedBy + "]";
    }
    
}

InventoryOrder.java:

@Entity
@Table(name = "inventory_orders")
public class InventoryOrder {

    @Id
    private int id;
    
    @OneToOne(cascade = CascadeType.ALL)
    @MapsId
    @JoinColumn(name = "inventory_id")
    private Inventory inventory;
    
    @Column(name = "order_id")
    private Integer orderId;
    
    @ManyToOne
    @JoinColumn(name = "reservation_id")
    private Reservation reservation;
    
    @Column(name = "product_id")
    private int productId;
    
    @Column(name = "quantity")
    private int quantity;
    
    @Column(name = "price_at_purchase")
    private BigDecimal priceAtPurchase;

    public InventoryOrder() {
    
    }
    
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Inventory getInventory() {
        return inventory;
    }

    public void setInventory(Inventory inventory) {
        this.inventory = inventory;
    }

    public Integer getOrderId() {
        return orderId;
    }

    public void setOrderId(Integer orderId) {
        this.orderId = orderId;
    }

    public Reservation getReservation() {
        return reservation;
    }

    public void setReservation(Reservation reservation) {
        this.reservation = reservation;
    }
    
    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public BigDecimal getPriceAtPurchase() {
        return priceAtPurchase;
    }

    public void setPriceAtPurchase(BigDecimal priceAtPurchase) {
        this.priceAtPurchase = priceAtPurchase;
    }

    @Override
    public String toString() {
        return "InventoryOrder [id=" + id + ", inventory=" + inventory + ", orderId=" + orderId
                + ", reservation=" + reservation + ", productId=" + productId + ", quantity=" + quantity
                + ", priceAtPurchase=" + priceAtPurchase + "]";
    }
    
}

Order.java:

@Entity
@Table(name = "orders")
public class Order {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "order_id")
    private int orderId;

    @Column(name = "user_id")
    private int userId;
    
    @Column(name = "total_amount")
    private BigDecimal totalAmount;
    
    @Column(name = "order_status")
    private String orderStatus;

    @Column(name = "order_creation_datetime", insertable = false, updatable = false)
    private LocalDateTime orderCreationDateTime;

    @Column(name = "order_cancellation_datetime",  insertable = false, updatable = false)
    private LocalDateTime orderCancellationDateTime;
    
    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "order_id")
    private List<OrderItem> orderItems;
    
    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "order_id")
    private List<InventoryOrder> inventoryOrders;
    
    public Order() {

    }

    public int getOrderId() {
        return orderId;
    }

    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public BigDecimal getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }

    public OrderStatus getOrderStatus() {
        return OrderStatus.valueOf(orderStatus);
    }

    public void setOrderStatus(OrderStatus orderStatus) {
        this.orderStatus = orderStatus.toString();
    }

    public LocalDateTime getOrderCreationDateTime() {
        return orderCreationDateTime;
    }

    public void setOrderCreationDateTime(LocalDateTime orderCreationDateTime) {
        this.orderCreationDateTime = orderCreationDateTime;
    }

    public LocalDateTime getOrderCancellationDateTime() {
        return orderCancellationDateTime;
    }

    public void setOrderCancellationDateTime(LocalDateTime orderCancellationDateTime) {
        this.orderCancellationDateTime = orderCancellationDateTime;
    }
    
    public List<OrderItem> getOrderItems() {
        return orderItems;
    }

    public void setOrderItems(List<OrderItem> orderItems) {
        this.orderItems = orderItems;
    }

    public void setOrderStatus(String orderStatus) {
        this.orderStatus = orderStatus;
    }
    
    public List<InventoryOrder> getInventoryOrders() {
        return inventoryOrders;
    }

    public void setInventoryOrders(List<InventoryOrder> inventoryOrders) {
        this.inventoryOrders = inventoryOrders;
    }

    public void addOrderItem(OrderItem orderItem) {
        if(orderItems == null) {
            orderItems = new ArrayList<>();
        }
        orderItems.add(orderItem);
    }

    public void addInventoryOrder(InventoryOrder inventoryOrder) {
        if(inventoryOrders == null) {
            inventoryOrders = new ArrayList<>();
        }
        inventoryOrders.add(inventoryOrder);
    }

    
    @Override
    public String toString() {
        return "Order [orderId=" + orderId + ", userId=" + userId + ", totalAmount=" + totalAmount + ", orderStatus="
                + orderStatus + "]";
    }

}

OrderItem.java:

@Entity
@Table(name = "order_items")
public class OrderItem {
    
    @EmbeddedId
    private OrderItemKey orderItemKey;
    
    @Column(name = "quantity")
    private int quantity;
    
    @Column(name = "price_at_purchase")
    private BigDecimal priceAtPurchase;
    
    public OrderItem() {
    
    }
        
    public OrderItem(int orderId, int productId, int quantity, BigDecimal priceAtPurchase) {
        this.orderItemKey = new OrderItemKey(orderId, productId);
        this.quantity = quantity;
        this.priceAtPurchase = priceAtPurchase;
    }

    public OrderItemKey getOrderItemKey() {
        return orderItemKey;
    }

    public void setOrderItemKey(OrderItemKey orderItemKey) {
        this.orderItemKey = orderItemKey;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public BigDecimal getPriceAtPurchase() {
        return priceAtPurchase;
    }

    public void setPriceAtPurchase(BigDecimal priceAtPurchase) {
        this.priceAtPurchase = priceAtPurchase;
    }

    @Override
    public String toString() {
        return "OrderItem [orderItemKey=" + orderItemKey + ", quantity=" + quantity + ", priceAtPurchase="
                + priceAtPurchase + "]";
    }

}

OrderItemKey.java:

@Embeddable
public class OrderItemKey implements Serializable {
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Column(name = "order_id")
    private int orderId;
    
    @Column(name = "product_id")
    private int productId;

    public OrderItemKey() {
    
    }

    public OrderItemKey(int orderId, int productId) {
        this.orderId = orderId;
        this.productId = productId;
    }

    public int getOrderId() {
        return orderId;
    }

    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    @Override
    public int hashCode() {
        return Objects.hash(orderId, productId);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        OrderItemKey other = (OrderItemKey) obj;
        return orderId == other.orderId && productId == other.productId;
    }

    @Override
    public String toString() {
        return "OrderItemKey [orderId=" + orderId + ", productId=" + productId + "]";
    }

}

Reservation.java:

@Entity
@Table(name = "reservations")
public class Reservation {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "reservation_id")
    private int reservationId;
    
    @Column(name = "user_id")
    private int userId;
    
    @Column(name = "creation_datetime", insertable = false, updatable = false)
    private LocalDateTime creationDateTime;

    @Column(name = "expiration_datetime", insertable = false, updatable = false)
    private LocalDateTime expirationDateTime;
    
    @Column(name = "status")
    private String status;
    
    @OneToMany(mappedBy = "reservation", cascade = CascadeType.ALL)
    private List<InventoryOrder> inventoryOrders;

    public Reservation() {
    
    }

    public Reservation(int userId, ReservationEntityStatus status) {
        this.userId = userId;
        this.status = status.name();
    }

    public int getReservationId() {
        return reservationId;
    }

    public void setReservationId(int reservationId) {
        this.reservationId = reservationId;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public LocalDateTime getCreationDateTime() {
        return creationDateTime;
    }

    public void setCreationDateTime(LocalDateTime creationDateTime) {
        this.creationDateTime = creationDateTime;
    }

    public LocalDateTime getExpirationDateTime() {
        return expirationDateTime;
    }

    public void setExpirationDateTime(LocalDateTime expirationDateTime) {
        this.expirationDateTime = expirationDateTime;
    }

    public ReservationEntityStatus getStatus() {
        return ReservationEntityStatus.valueOf(status);
    }

    public void setStatus(ReservationEntityStatus status) {
        this.status = status.name();
    }

    public void setStatus(String status) {
        this.status = status;
    }
    
    public List<InventoryOrder> getInventoryOrders() {
        return inventoryOrders;
    }

    public void setInventoryOrders(List<InventoryOrder> inventoryOrders) {
        this.inventoryOrders = inventoryOrders;
    }
    
    public void addInventoryOrder(InventoryOrder inventoryOrder) {
        if(inventoryOrders == null) {
            inventoryOrders = new ArrayList<>();
        }
        inventoryOrders.add(inventoryOrder);
    }

    @Override
    public String toString() {
        return "Reservation [reservationId=" + reservationId + ", userId=" + userId + ", creationDateTime="
                + creationDateTime + ", expirationDateTime=" + expirationDateTime + ", status=" + status + "]";
    }

}

ReservationDaoImpl.java:

@Repository
public class ReservationDAOImpl implements ReservationDAO {
    
    @Autowired
    private SessionFactory sessionFactory;
    
    @Override
    public int getReservationId(int userId, List<CartItem> validatedCartItems, List<Inventory> inventories) {
        Reservation reservation = new Reservation(userId, ReservationEntityStatus.ACTIVE);
        
        Session currentSession = sessionFactory.getCurrentSession();
        
        currentSession.save(reservation);
        System.out.println(reservation);
        int reservationId = reservation.getReservationId(), i = 0;
        
        
        for (CartItem cartItem : validatedCartItems) {
            Inventory inventory = inventories.get(i);
            InventoryOrder inventoryOrder = new InventoryOrder();
            inventoryOrder.setInventory(inventory);
            inventoryOrder.setReservation(reservation);
            inventoryOrder.setProductId(cartItem.getProductId());
            inventoryOrder.setQuantity(cartItem.getQuantity());
            inventoryOrder.setPriceAtPurchase(cartItem.getPricePerUnit());
            
            System.out.println(inventoryOrder);

            // Synchronize the relationship
            reservation.addInventoryOrder(inventoryOrder);
            
            i++;
        }
        
        return reservationId;
    }
    
}

OrderDAOImpl.java:

@Repository
public class OrderDAOImpl implements OrderDAO {
    
    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public Order placeOrder(int reservationId) {
        Session currentSession = sessionFactory.getCurrentSession();
        
        Reservation reservation = currentSession.get(Reservation.class, reservationId);
        
        if(reservation == null)
            throw new InvalidReservationIdException("Reservation id: " + reservationId + " is invalid!");
        
        boolean reservationExpired = !reservation.getExpirationDateTime().isAfter(LocalDateTime.now());
        System.out.println(reservation);
        System.out.println("reservationExpired: " + reservationExpired);
        
        if(reservationExpired)
            throw new InvalidReservationIdException("Reservation id: " + reservationId + " has expired!");
        
        if(reservation.getStatus() == ReservationEntityStatus.USED)
            throw new InvalidReservationIdException("Reservation id: " + reservationId + " has already been used!");
        
        
        System.out.println("Getting inventory orders...");
        
        List<InventoryOrder> inventoryOrders = reservation.getInventoryOrders();
        
        System.out.println(inventoryOrders);
        
        BigDecimal totalAmount = BigDecimal.ZERO;
        for(InventoryOrder inventoryOrder : inventoryOrders) {
            BigDecimal priceAtPurchase = inventoryOrder.getPriceAtPurchase();
            BigDecimal quantity = BigDecimal.valueOf(inventoryOrder.getQuantity());
            totalAmount = priceAtPurchase.multiply(quantity).add(totalAmount);
        };
        
        Order order = new Order();
        order.setUserId(reservation.getUserId());
        order.setTotalAmount(totalAmount);
        order.setOrderStatus(OrderStatus.CONFIRMED);
        
        int orderId = (int)currentSession.save(order);
        
        System.out.println("orderId: " + orderId);
        
        inventoryOrders.stream().forEach(inventoryOrder -> {
            Inventory inventory = inventoryOrder.getInventory();
            inventory.setChangeType(ChangeType.SALE);
            inventory.setStatus(InventoryStatus.COMPLETED);
            
            OrderItem orderItem = new OrderItem(orderId, inventoryOrder.getProductId(), 
                    inventoryOrder.getQuantity(), inventoryOrder.getPriceAtPurchase());
            System.out.println(orderItem);
            System.out.println(inventoryOrder.getInventory());
            // Synchronize the relationship
            order.addInventoryOrder(inventoryOrder);
            order.addOrderItem(orderItem);
        });
        
        System.out.println(order.getOrderItems());
        System.out.println(order.getInventoryOrders());
        reservation.setStatus(ReservationEntityStatus.USED);
        currentSession.save(reservation);
        
        return order;
    }

}

When I hit the api to reserve-items then the getReservationId() in ReservationDAOImpl is called. And below is the console logs for that method showing the hibernate queries.

Hibernate: insert into inventory (adjusted_by, change_type, product_id, quantity, reason, status, status_change_datetime) values (?, ?, ?, ?, ?, ?, ?)
Inventory [inverntoryId=1000037, productId=1000016, changeType=RESERVATION, quantity=5, reason=null, status=ACTIVE, statusChangeDatetime=null, adjustedBy=null]
Hibernate: insert into inventory (adjusted_by, change_type, product_id, quantity, reason, status, status_change_datetime) values (?, ?, ?, ?, ?, ?, ?)
Inventory [inverntoryId=1000038, productId=1000003, changeType=RESERVATION, quantity=1, reason=null, status=ACTIVE, statusChangeDatetime=null, adjustedBy=null]
Hibernate: insert into inventory (adjusted_by, change_type, product_id, quantity, reason, status, status_change_datetime) values (?, ?, ?, ?, ?, ?, ?)
Inventory [inverntoryId=1000039, productId=1000014, changeType=RESERVATION, quantity=2, reason=null, status=ACTIVE, statusChangeDatetime=null, adjustedBy=null]
Hibernate: insert into reservations (status, user_id) values (?, ?)
Reservation [reservationId=1000013, userId=1000001, creationDateTime=null, expirationDateTime=null, status=ACTIVE]
InventoryOrder [id=0, inventory=Inventory [inverntoryId=1000037, productId=1000016, changeType=RESERVATION, quantity=5, reason=null, status=ACTIVE, statusChangeDatetime=null, adjustedBy=null], orderId=null, reservation=Reservation [reservationId=1000013, userId=1000001, creationDateTime=null, expirationDateTime=null, status=ACTIVE], productId=1000016, quantity=5, priceAtPurchase=20.00]
InventoryOrder [id=0, inventory=Inventory [inverntoryId=1000038, productId=1000003, changeType=RESERVATION, quantity=1, reason=null, status=ACTIVE, statusChangeDatetime=null, adjustedBy=null], orderId=null, reservation=Reservation [reservationId=1000013, userId=1000001, creationDateTime=null, expirationDateTime=null, status=ACTIVE], productId=1000003, quantity=1, priceAtPurchase=2999.00]
InventoryOrder [id=0, inventory=Inventory [inverntoryId=1000039, productId=1000014, changeType=RESERVATION, quantity=2, reason=null, status=ACTIVE, statusChangeDatetime=null, adjustedBy=null], orderId=null, reservation=Reservation [reservationId=1000013, userId=1000001, creationDateTime=null, expirationDateTime=null, status=ACTIVE], productId=1000014, quantity=2, priceAtPurchase=93.00]
reservationId: 1000013
Hibernate: insert into inventory_orders (order_id, price_at_purchase, product_id, quantity, reservation_id, inventory_id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into inventory_orders (order_id, price_at_purchase, product_id, quantity, reservation_id, inventory_id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into inventory_orders (order_id, price_at_purchase, product_id, quantity, reservation_id, inventory_id) values (?, ?, ?, ?, ?, ?)
Hibernate: update inventory set adjusted_by=?, change_type=?, product_id=?, quantity=?, reason=?, status=?, status_change_datetime=? where inventory_id=?

And for place-order api the placeOrder() in OrderDAOImpl is called. And its console logs for that method are shown below:

Hibernate: select reservatio0_.reservation_id as reservat1_6_0_, reservatio0_.creation_datetime as creation2_6_0_, reservatio0_.expiration_datetime as expirati3_6_0_, reservatio0_.status as status4_6_0_, reservatio0_.user_id as user_id5_6_0_ from reservations reservatio0_ where reservatio0_.reservation_id=?
Reservation [reservationId=1000013, userId=1000001, creationDateTime=2024-11-28T15:04:05, expirationDateTime=2024-11-28T15:34:05, status=ACTIVE]
reservationExpired: false
Getting inventory orders...
Hibernate: select inventoryo0_.reservation_id as reservat6_2_0_, inventoryo0_.inventory_id as inventor1_2_0_, inventoryo0_.inventory_id as inventor1_2_1_, inventoryo0_.order_id as order_id2_2_1_, inventoryo0_.price_at_purchase as price_at3_2_1_, inventoryo0_.product_id as product_4_2_1_, inventoryo0_.quantity as quantity5_2_1_, inventoryo0_.reservation_id as reservat6_2_1_, inventory1_.inventory_id as inventor1_1_2_, inventory1_.adjusted_by as adjusted2_1_2_, inventory1_.change_type as change_t3_1_2_, inventory1_.product_id as product_4_1_2_, inventory1_.quantity as quantity5_1_2_, inventory1_.reason as reason6_1_2_, inventory1_.status as status7_1_2_, inventory1_.status_change_datetime as status_c8_1_2_ from inventory_orders inventoryo0_ inner join inventory inventory1_ on inventoryo0_.inventory_id=inventory1_.inventory_id where inventoryo0_.reservation_id=?
[InventoryOrder [id=1000037, inventory=Inventory [inverntoryId=1000037, productId=1000016, changeType=RESERVATION, quantity=5, reason=null, status=ACTIVE, statusChangeDatetime=null, adjustedBy=null], orderId=null, reservation=Reservation [reservationId=1000013, userId=1000001, creationDateTime=2024-11-28T15:04:05, expirationDateTime=2024-11-28T15:34:05, status=ACTIVE], productId=1000016, quantity=5, priceAtPurchase=20.00], InventoryOrder [id=1000038, inventory=Inventory [inverntoryId=1000038, productId=1000003, changeType=RESERVATION, quantity=1, reason=null, status=ACTIVE, statusChangeDatetime=null, adjustedBy=null], orderId=null, reservation=Reservation [reservationId=1000013, userId=1000001, creationDateTime=2024-11-28T15:04:05, expirationDateTime=2024-11-28T15:34:05, status=ACTIVE], productId=1000003, quantity=1, priceAtPurchase=2999.00], InventoryOrder [id=1000039, inventory=Inventory [inverntoryId=1000039, productId=1000014, changeType=RESERVATION, quantity=2, reason=null, status=ACTIVE, statusChangeDatetime=null, adjustedBy=null], orderId=null, reservation=Reservation [reservationId=1000013, userId=1000001, creationDateTime=2024-11-28T15:04:05, expirationDateTime=2024-11-28T15:34:05, status=ACTIVE], productId=1000014, quantity=2, priceAtPurchase=93.00]]
Hibernate: insert into orders (order_status, total_amount, user_id) values (?, ?, ?)
orderId: 1000002
OrderItem [orderItemKey=OrderItemKey [orderId=1000002, productId=1000016], quantity=5, priceAtPurchase=20.00]
Inventory [inverntoryId=1000037, productId=1000016, changeType=SALE, quantity=5, reason=null, status=COMPLETED, statusChangeDatetime=null, adjustedBy=null]
OrderItem [orderItemKey=OrderItemKey [orderId=1000002, productId=1000003], quantity=1, priceAtPurchase=2999.00]
Inventory [inverntoryId=1000038, productId=1000003, changeType=SALE, quantity=1, reason=null, status=COMPLETED, statusChangeDatetime=null, adjustedBy=null]
OrderItem [orderItemKey=OrderItemKey [orderId=1000002, productId=1000014], quantity=2, priceAtPurchase=93.00]
Inventory [inverntoryId=1000039, productId=1000014, changeType=SALE, quantity=2, reason=null, status=COMPLETED, statusChangeDatetime=null, adjustedBy=null]
[OrderItem [orderItemKey=OrderItemKey [orderId=1000002, productId=1000016], quantity=5, priceAtPurchase=20.00], OrderItem [orderItemKey=OrderItemKey [orderId=1000002, productId=1000003], quantity=1, priceAtPurchase=2999.00], OrderItem [orderItemKey=OrderItemKey [orderId=1000002, productId=1000014], quantity=2, priceAtPurchase=93.00]]
[InventoryOrder [id=1000037, inventory=Inventory [inverntoryId=1000037, productId=1000016, changeType=SALE, quantity=5, reason=null, status=COMPLETED, statusChangeDatetime=null, adjustedBy=null], orderId=null, reservation=Reservation [reservationId=1000013, userId=1000001, creationDateTime=2024-11-28T15:04:05, expirationDateTime=2024-11-28T15:34:05, status=ACTIVE], productId=1000016, quantity=5, priceAtPurchase=20.00], InventoryOrder [id=1000038, inventory=Inventory [inverntoryId=1000038, productId=1000003, changeType=SALE, quantity=1, reason=null, status=COMPLETED, statusChangeDatetime=null, adjustedBy=null], orderId=null, reservation=Reservation [reservationId=1000013, userId=1000001, creationDateTime=2024-11-28T15:04:05, expirationDateTime=2024-11-28T15:34:05, status=ACTIVE], productId=1000003, quantity=1, priceAtPurchase=2999.00], InventoryOrder [id=1000039, inventory=Inventory [inverntoryId=1000039, productId=1000014, changeType=SALE, quantity=2, reason=null, status=COMPLETED, statusChangeDatetime=null, adjustedBy=null], orderId=null, reservation=Reservation [reservationId=1000013, userId=1000001, creationDateTime=2024-11-28T15:04:05, expirationDateTime=2024-11-28T15:34:05, status=ACTIVE], productId=1000014, quantity=2, priceAtPurchase=93.00]]
Order [orderId=1000002, userId=1000001, totalAmount=3285.00, orderStatus=CONFIRMED]
Hibernate: select orderitem_.order_id, orderitem_.product_id, orderitem_.price_at_purchase as price_at3_3_, orderitem_.quantity as quantity4_3_ from order_items orderitem_ where orderitem_.order_id=? and orderitem_.product_id=?
Hibernate: select orderitem_.order_id, orderitem_.product_id, orderitem_.price_at_purchase as price_at3_3_, orderitem_.quantity as quantity4_3_ from order_items orderitem_ where orderitem_.order_id=? and orderitem_.product_id=?
Hibernate: select orderitem_.order_id, orderitem_.product_id, orderitem_.price_at_purchase as price_at3_3_, orderitem_.quantity as quantity4_3_ from order_items orderitem_ where orderitem_.order_id=? and orderitem_.product_id=?
Hibernate: insert into order_items (price_at_purchase, quantity, order_id, product_id) values (?, ?, ?, ?)
Hibernate: insert into order_items (price_at_purchase, quantity, order_id, product_id) values (?, ?, ?, ?)
Hibernate: insert into order_items (price_at_purchase, quantity, order_id, product_id) values (?, ?, ?, ?)
Hibernate: update inventory set adjusted_by=?, change_type=?, product_id=?, quantity=?, reason=?, status=?, status_change_datetime=? where inventory_id=?
Hibernate: update inventory set adjusted_by=?, change_type=?, product_id=?, quantity=?, reason=?, status=?, status_change_datetime=? where inventory_id=?
Hibernate: update inventory set adjusted_by=?, change_type=?, product_id=?, quantity=?, reason=?, status=?, status_change_datetime=? where inventory_id=?
Hibernate: update reservations set status=?, user_id=? where reservation_id=?
Hibernate: update inventory_orders set order_id=? where inventory_id=?
Hibernate: update inventory_orders set order_id=? where inventory_id=?Hibernate: update inventory_orders set order_id=? where inventory_id=?
Hibernate: update order_items set order_id=? where order_id=? and product_id=?
Hibernate: update order_items set order_id=? where order_id=? and product_id=?
Hibernate: update order_items set order_id=? where order_id=? and product_id=?

I am seeing extra queries are being issued for both api calls.

For reserve-items api why the last query 'update inventory ... ' is there?

Similarly for place-order api why 3 'update order_items ...' at the end when 3 OrderItem entities have already been inserted into the db? And I also see 3 select queries from order_items table before the OrderItem entities are inserted. Why so?

InventoryOrder has a one-to-one uni-directional relationship with Inventory. And Inventory order primary key is same as the primary key of Inventory so I used @MapsId to map the identifier of InventoryOrder which is 'id' with the pk of Inventory.

InventoryOrder also has a many-to-one bi-directional relationship with Reservation entity and a many-to-one uni-directional relationship with Order entity.

Order to OrderItem is a one-to-many uni-directional relationship.

I know this is a long post. But I am struggling at why these extra queries are there and is there any way to optimize it.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论