- Add @ManyToOne(fetch = LAZY) + @JoinColumn(name = "user_id",
insertable = false, updatable = false) to Order entity so ORM can
navigate order.getUser().getEmail() for admin responses
- Keep userId as writable UUID field; the relationship is read-only
to preserve backward compatibility with existing setUserId() calls
- Add getUser() / setUser() accessors
- Replace handwritten @Query JOIN FETCH with Spring Data derived method
findAllByOrderByCreatedAtDesc() annotated with @EntityGraph(attributePaths
= {"user"}) — same eager-load behavior, zero custom JPQL
- No database schema change: user_id FK already exists
140 lines
3.1 KiB
Java
140 lines
3.1 KiB
Java
package se.bilhalsning.entity;
|
|
|
|
import jakarta.persistence.Column;
|
|
import jakarta.persistence.Entity;
|
|
import jakarta.persistence.FetchType;
|
|
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 java.math.BigDecimal;
|
|
import java.time.Instant;
|
|
import java.util.UUID;
|
|
|
|
@Entity
|
|
@Table(name = "orders")
|
|
public class Order {
|
|
|
|
@Id
|
|
@Column(name = "id", columnDefinition = "uuid", nullable = false, updatable = false)
|
|
private UUID id;
|
|
|
|
@Column(name = "user_id", nullable = false, columnDefinition = "uuid")
|
|
private UUID userId;
|
|
|
|
@ManyToOne(fetch = FetchType.LAZY)
|
|
@JoinColumn(name = "user_id", insertable = false, updatable = false)
|
|
private User user;
|
|
|
|
@Column(name = "plate", nullable = false, length = 10)
|
|
private String plate;
|
|
|
|
@Column(name = "letter_text", nullable = false, columnDefinition = "text")
|
|
private String letterText;
|
|
|
|
@Column(name = "status", nullable = false, length = 30)
|
|
private OrderStatus status = OrderStatus.PENDING_PAYMENT;
|
|
|
|
@Column(name = "amount_paid", precision = 10, scale = 2)
|
|
private BigDecimal amountPaid;
|
|
|
|
@Column(name = "tracking_id", length = 100)
|
|
private String trackingId;
|
|
|
|
@Column(name = "created_at", nullable = false)
|
|
private Instant createdAt;
|
|
|
|
@Column(name = "updated_at", nullable = false)
|
|
private Instant updatedAt;
|
|
|
|
@PrePersist
|
|
void onCreate() {
|
|
if (this.id == null) {
|
|
this.id = UUID.randomUUID();
|
|
}
|
|
Instant now = Instant.now();
|
|
if (this.createdAt == null) {
|
|
this.createdAt = now;
|
|
}
|
|
this.updatedAt = now;
|
|
}
|
|
|
|
@PreUpdate
|
|
void onUpdate() {
|
|
this.updatedAt = Instant.now();
|
|
}
|
|
|
|
public UUID getId() {
|
|
return id;
|
|
}
|
|
|
|
public void setId(UUID id) {
|
|
this.id = id;
|
|
}
|
|
|
|
public UUID getUserId() {
|
|
return userId;
|
|
}
|
|
|
|
public void setUserId(UUID userId) {
|
|
this.userId = userId;
|
|
}
|
|
|
|
public User getUser() {
|
|
return user;
|
|
}
|
|
|
|
public void setUser(User user) {
|
|
this.user = user;
|
|
}
|
|
|
|
public String getPlate() {
|
|
return plate;
|
|
}
|
|
|
|
public void setPlate(String plate) {
|
|
this.plate = plate;
|
|
}
|
|
|
|
public String getLetterText() {
|
|
return letterText;
|
|
}
|
|
|
|
public void setLetterText(String letterText) {
|
|
this.letterText = letterText;
|
|
}
|
|
|
|
public OrderStatus getStatus() {
|
|
return status;
|
|
}
|
|
|
|
public void setStatus(OrderStatus status) {
|
|
this.status = status;
|
|
}
|
|
|
|
public BigDecimal getAmountPaid() {
|
|
return amountPaid;
|
|
}
|
|
|
|
public void setAmountPaid(BigDecimal amountPaid) {
|
|
this.amountPaid = amountPaid;
|
|
}
|
|
|
|
public String getTrackingId() {
|
|
return trackingId;
|
|
}
|
|
|
|
public void setTrackingId(String trackingId) {
|
|
this.trackingId = trackingId;
|
|
}
|
|
|
|
public Instant getCreatedAt() {
|
|
return createdAt;
|
|
}
|
|
|
|
public Instant getUpdatedAt() {
|
|
return updatedAt;
|
|
}
|
|
}
|