Drop Configurations correctly, reimplement OrderItem correctly

This commit is contained in:
Tim
2025-05-24 13:02:55 +02:00
parent 1572543c2e
commit 252ae36b86
3 changed files with 32 additions and 25 deletions

View File

@@ -18,6 +18,6 @@ public class Order {
@Column(name = "time")
private Long time;
@OneToMany(mappedBy = "orderId")
@OneToMany(mappedBy = "order", fetch = FetchType.LAZY)
private List<OrderItem> orderItems;
}

View File

@@ -2,17 +2,23 @@ package de.htwsaar.webshop.repository.entities;
import jakarta.persistence.*;
import jakarta.validation.constraints.Min;
import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.OnDelete;
import java.io.Serializable;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "OrderItems")
public class OrderItem {
@EmbeddedId
private OrderItemId id;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
@Min(value = 1, message = "Amount must be at least 1")
@@ -29,22 +35,4 @@ public class OrderItem {
@JoinColumn(name = "articleId", referencedColumnName = "id", nullable = false)
@OnDelete(action = org.hibernate.annotations.OnDeleteAction.SET_NULL)
private Article article;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("articleConfId")
@JoinColumn(name = "articleConfId", referencedColumnName = "id")
@OnDelete(action = org.hibernate.annotations.OnDeleteAction.SET_NULL)
private ArticleConfiguration articleConfig;
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@EqualsAndHashCode
@Embeddable
class OrderItemId implements Serializable {
private Long orderId;
private Long articleId;
private Long articleConfId;
}
}

View File

@@ -1 +1,20 @@
DROP TABLE IF EXISTS ArticleConfigurations;
DROP TABLE IF EXISTS ArticleConfigurations;
DROP TABLE OrderItems;
CREATE TABLE IF NOT EXISTS OrderItems
(
id INTEGER NOT NULL PRIMARY KEY, --composite pk was wrong
orderId INTEGER NOT NULL,
articleId INTEGER NOT NULL,
amount INTEGER NOT NULL,
-- art conf not needed
CONSTRAINT cAmount CHECK (amount > 0),
FOREIGN KEY (orderId) REFERENCES Orders (id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (articleId) REFERENCES Articles (id)
ON DELETE SET NULL
ON UPDATE CASCADE
);