Class Product

java.lang.Object
com.example.springrest.models.Product

@Entity public class Product extends Object
Domain entity representing a product in the catalog.

Persistence: Mapped to the products table with an auto-incremented primary key. Uses GenerationType.IDENTITY for portability with common RDBMS engines.

Validation: - name must be non-blank and ≤ 120 chars.
- price must be provided; database column enforces precision (12,2).

Typical usage:


 // Create and persist a product
 Product p = new Product("Coffee Mug", new BigDecimal("12.99"));
 productRepo.save(p);

 // Retrieve or fail fast
 Product found = productService.getOrThrow(p.getId());

 // Update through service (recommended; enforces business rules)
 productService.update(p.getId(), prod -> {
     prod.setPrice(new BigDecimal("10.99"));
 });
 

Design notes: Keep business logic in services. The entity holds only simple invariants and mappings.

Since:
1.0
  • Constructor Details

    • Product

      public Product(String name, BigDecimal price)
      Convenience constructor for creating new products.
      Parameters:
      name - non-blank product name (≤ 120 chars)
      price - price with up to two decimal places
      Throws:
      IllegalArgumentException - if name is blank or price is null