feat: Implement order items entity and database migration

- Introduce OrderItems entity with persistence capabilities.
- Establish a one-to-many relationship between Orders and OrderItems.
- Update Orders entity to reflect the relationship change.
- Create a Flyway migration script to create the order_items table and drop the jsonb items column from the orders table.
This commit is contained in:
2026-02-26 18:13:58 -03:00
parent 5a7f447d96
commit 82d6a3439a
3 changed files with 33 additions and 2 deletions

View File

@@ -1,9 +1,13 @@
package com.vegamarket.orderservice.dtos;
import com.vegamarket.orderservice.model.Orders;
import jakarta.persistence.*;
import lombok.*;
import java.math.BigDecimal;
@Entity
@Table(name = "order_items")
@Getter
@Setter
@AllArgsConstructor
@@ -11,6 +15,15 @@ import java.math.BigDecimal;
@Builder
public class OrderItems {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long orderItemId;
private Long productId;
private Long quantity;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "order_id", nullable = false)
private Orders order;
private String name;
private BigDecimal price;
}

View File

@@ -34,8 +34,11 @@ public class Orders {
private BigDecimal totalAmount;
private String traceId;
@Column(columnDefinition = "jsonb", nullable = false)
@JdbcTypeCode(SqlTypes.JSON)
@OneToMany(
mappedBy = "order",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<OrderItems> items;
@CreationTimestamp

View File

@@ -0,0 +1,15 @@
alter table orders
drop column items;
create table order_items (
order_item_id bigint generated always as identity primary key,
productId bigint not null,
quantity int not null,
order_id bigint not null,
name varchar(255) not null,
price numeric(19,2) not null,
constraint fk_order_item_order
foreign key (order_id)
references orders(order_id)
on delete cascade
);