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:
@@ -1,9 +1,13 @@
|
|||||||
package com.vegamarket.orderservice.dtos;
|
package com.vegamarket.orderservice.dtos;
|
||||||
|
|
||||||
|
import com.vegamarket.orderservice.model.Orders;
|
||||||
|
import jakarta.persistence.*;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "order_items")
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@@ -11,6 +15,15 @@ import java.math.BigDecimal;
|
|||||||
@Builder
|
@Builder
|
||||||
public class OrderItems {
|
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 String name;
|
||||||
private BigDecimal price;
|
private BigDecimal price;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,8 +34,11 @@ public class Orders {
|
|||||||
private BigDecimal totalAmount;
|
private BigDecimal totalAmount;
|
||||||
private String traceId;
|
private String traceId;
|
||||||
|
|
||||||
@Column(columnDefinition = "jsonb", nullable = false)
|
@OneToMany(
|
||||||
@JdbcTypeCode(SqlTypes.JSON)
|
mappedBy = "order",
|
||||||
|
cascade = CascadeType.ALL,
|
||||||
|
orphanRemoval = true
|
||||||
|
)
|
||||||
private List<OrderItems> items;
|
private List<OrderItems> items;
|
||||||
|
|
||||||
@CreationTimestamp
|
@CreationTimestamp
|
||||||
|
|||||||
@@ -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
|
||||||
|
);
|
||||||
Reference in New Issue
Block a user