Product.java
spring_boot/code/intermediate/src/main/java/com/example/demo/entity/Product.java
package com.example.demo.entity;
import jakarta.persistence.*;
import java.math.BigDecimal;
import java.time.Instant;
/**
* JPA entity representing a product in the database.
* <p>
* Mapped to the {@code products} table. Uses {@code IDENTITY} for primary key generation
* (database auto-increment). {@code @PrePersist} sets {@code createdAt} before the first persist.
*/
@Entity
@Table(name = "products")
public class Product {
/** Primary key; auto-generated by the database (IDENTITY strategy). */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/** Product name; required, max 255 characters. */
@Column(nullable = false, length = 255)
private String name;
/** Price; required, stored with precision 19 and scale 2 (e.g. for currency). */
@Column(nullable = false, precision = 19, scale = 2)
private BigDecimal price;
/** Stock quantity; non-negative. */
private int stock;
/** Timestamp when the product was created; set once and not updated. */
@Column(name = "created_at", updatable = false)
private Instant createdAt;
/**
* JPA lifecycle callback: set {@code createdAt} to current time before the entity is first persisted.
*/
@PrePersist
protected void onCreate() {
createdAt = Instant.now();
}
/** No-argument constructor; required by JPA. */
public Product() {}
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public BigDecimal getPrice() { return price; }
public void setPrice(BigDecimal price) { this.price = price; }
public int getStock() { return stock; }
public void setStock(int stock) { this.stock = stock; }
public Instant getCreatedAt() { return createdAt; }
public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }
}
Articles liés
DemoApplication.java
DemoApplication.java — java source code from the spring boot learning materials (spring_boot/code/advanced/src/main/java/com/example/demo/DemoApplication.java).
Lire l'article →CustomHealthIndicator.java
CustomHealthIndicator.java — java source code from the spring boot learning materials (spring_boot/code/advanced/src/main/java/com/example/demo/actuator/CustomHealthIndicator.java).
Lire l'article →SecurityConfig.java
SecurityConfig.java — java source code from the spring boot learning materials (spring_boot/code/advanced/src/main/java/com/example/demo/config/SecurityConfig.java).
Lire l'article →ProductController.java
ProductController.java — java source code from the spring boot learning materials (spring_boot/code/advanced/src/main/java/com/example/demo/controller/ProductController.java).
Lire l'article →PublicController.java
PublicController.java — java source code from the spring boot learning materials (spring_boot/code/advanced/src/main/java/com/example/demo/controller/PublicController.java).
Lire l'article →ProductCreateRequest.java
ProductCreateRequest.java — java source code from the spring boot learning materials (spring_boot/code/advanced/src/main/java/com/example/demo/dto/ProductCreateRequest.java).
Lire l'article →