CartItem.java
package com.rbctcsworld.ecommerce.cart;
import com.rbctcsworld.ecommerce.product.Product;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.PrePersist;
import jakarta.persistence.PreUpdate;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import java.time.LocalDateTime;
/** One product line in a customer's cart. A customer has at most one line per product. */
@Entity
@Table(name = "cart_items",
uniqueConstraints = @UniqueConstraint(name = "uk_cart_user_product", columnNames = {"user_id", "product_id"}))
public class CartItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "user_id", nullable = false)
private Long userId;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "product_id", nullable = false)
private Product product;
@Column(nullable = false)
private int quantity;
@Column(name = "added_at", nullable = false)
private LocalDateTime addedAt;
@Column(name = "updated_at", nullable = false)
private LocalDateTime updatedAt;
protected CartItem() {
}
public CartItem(Long userId, Product product, int quantity) {
this.userId = userId;
this.product = product;
this.quantity = quantity;
}
@PrePersist
void onCreate() {
addedAt = LocalDateTime.now();
updatedAt = addedAt;
}
@PreUpdate
void onUpdate() {
updatedAt = LocalDateTime.now();
}
public Long getId() { return id; }
public Long getUserId() { return userId; }
public Product getProduct() { return product; }
public int getQuantity() { return quantity; }
public LocalDateTime getAddedAt() { return addedAt; }
public void setQuantity(int quantity) { this.quantity = quantity; }
}