Package com.example.springrest.models
Class Product
java.lang.Object
com.example.springrest.models.Product
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 Summary
ConstructorsConstructorDescriptionProduct
(String name, BigDecimal price) Convenience constructor for creating new products. -
Method Summary
-
Constructor Details
-
Product
Convenience constructor for creating new products.- Parameters:
name
- non-blank product name (≤ 120 chars)price
- price with up to two decimal places- Throws:
IllegalArgumentException
- ifname
is blank orprice
isnull
-