У меня 2 объекта.
Order и DetailOrder
Код: Выделить всё
@Table(name = "orders")
public class Order {
@Id
@Column("id")
private Long id;
@Column(value = "order_number")
private String orderNumber;
@Column(value = "total_amount")
private Long totalAmount;
@Column(value = "order_date")
private Date orderDate;
@Column(value = "customer_name")
private String customerName;
@Column(value = "address")
private String deliveryAddress;
@Column(value = "payment_type")
private String paymentType;
@Column(value = "delivery_type")
private String deliveryType;
@MappedCollection(idColumn = "order_id", keyColumn = "id")
private List detailOrders;
public Order(Long id, String orderNumber, Long totalAmount, Date orderDate, String customerName, String deliveryAddress, String paymentType, String deliveryType, List detailOrders) {
this.id = id;
this.orderNumber = orderNumber;
this.totalAmount = totalAmount;
this.orderDate = orderDate;
this.customerName = customerName;
this.deliveryAddress = deliveryAddress;
this.paymentType = paymentType;
this.deliveryType = deliveryType;
this.detailOrders = detailOrders;
}
Код: Выделить всё
@Table(name = "products")
public class DetailOrder {
@Id
private Long id;
@Column(value = "product_article")
private Long productArticle;
@Column(value = "product_name")
private String productName;
@Column(value = "product_amount")
private int productAmount;
@Column(value = "product_price")
private int productPrice;
@Column(value = "order_id")
private Long orderId;
public DetailOrder(Long id, Long productArticle, String productName, int productAmount, int productPrice, Long orderId) {
this.id = id;
this.productArticle = productArticle;
this.productName = productName;
this.productAmount = productAmount;
this.productPrice = productPrice;
this.orderId = orderId;
}
Код: Выделить всё
public interface OrderRepository extends ListCrudRepository {
List findByOrderDateAndTotalAmountGreaterThan(Date date, long amount);
}
Код: Выделить всё
public interface OrderService {
Order createOrder(Order order);
Optional getOrder(long id);
List findAll();
List findByOrderDateAndTotalAmountGreaterThan(Date date, long amount);
}
Код: Выделить всё
@Service
public class OrderServiceImpl implements OrderService {
private final OrderRepository orderRepository;
@Autowired
public OrderServiceImpl(OrderRepository orderRepository) {
this.orderRepository = orderRepository;
}
@Override
public Order createOrder(Order order) {
try {
order.setOrderNumber("generated");
order.setOrderDate(Date.valueOf(LocalDate.now()));
order.setTotalAmount(order.getDetailOrders().stream().mapToLong(detail -> detail.getProductPrice() * detail.getProductAmount()).sum());
return orderRepository.save(order);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Код: Выделить всё
@PostMapping("/orders")
public ResponseEntity createOrder(@RequestBody Order order) {
try {
Order response = orderService.createOrder(order);
System.out.println(response);
return new ResponseEntity(response, HttpStatus.CREATED);
} catch (Exception e) {
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Код: Выделить всё
CREATE TABLE products (
id BIGSERIAL PRIMARY KEY ,
product_article BIGINT NOT NULL,
product_name VARCHAR(255) NOT NULL,
product_amount INT NOT NULL,
product_price INT NOT NULL,
order_id BIGINT,
CONSTRAINT fk_order
FOREIGN KEY (order_id)
REFERENCES orders (id)
ON DELETE SET NULL
);
CREATE TABLE orders(
id BIGSERIAL PRIMARY KEY,
order_number varchar(255) not null,
total_amount bigint not null,
customer_name varchar(255) not null,
address varchar(255) not null,
delivery_type varchar(255),
payment_type varchar(255),
order_date date
)
Код: Выделить всё
{
"customerName": "Andrew Kramer",
"deliveryAddress": "London",
"paymentType": "Wired",
"deliveryType": "TEST",
"detailOrders": [
{
"productArticle": 52341252,
"productName": "test8",
"productAmount": 4,
"productPrice": 2000
},
{
"productArticle": 52341252,
"productName": "test9",
"productAmount": 2,
"productPrice": 2000
}
]
}
Код: Выделить всё
Failed to execute BatchWithValue{actions=[Insert{entity=DetailOrder{id=null, productArticle=12151,Код: Выделить всё
Executing prepared SQL statement [INSERT INTO "products" ("id", "order_id", "product_amount", "product_article", "product_name", "product_price") VALUES (?, ?, ?, ?, ?, ?)]
2024-12-02T21:44:36.809+03:00 TRACE 16736 --- [orders_service] [ main] o.s.jdbc.core.StatementCreatorUtils : Setting SQL statement parameter value: column index 1, parameter value [0], value class [java.lang.Integer], SQL type 4
2024-12-02T21:44:36.809+03:00 TRACE 16736 --- [orders_service] [ main] o.s.jdbc.core.StatementCreatorUtils : Setting SQL statement parameter value: column index 2, parameter value [25], value class [java.lang.Long], SQL type -5
2024-12-02T21:44:36.809+03:00 TRACE 16736 --- [orders_service] [ main] o.s.jdbc.core.StatementCreatorUtils : Setting SQL statement parameter value: column index 3, parameter value [4], value class [java.lang.Integer], SQL type 4
2024-12-02T21:44:36.809+03:00 TRACE 16736 --- [orders_service] [ main] o.s.jdbc.core.StatementCreatorUtils : Setting SQL statement parameter value: column index 4, parameter value [12151], value class [java.lang.Long], SQL type -5
2024-12-02T21:44:36.809+03:00 TRACE 16736 --- [orders_service] [ main] o.s.jdbc.core.StatementCreatorUtils : Setting SQL statement parameter value: column index 5, parameter value [Morojja], value class [java.lang.String], SQL type 12
2024-12-02T21:44:36.809+03:00 TRACE 16736 --- [orders_service] [ main] o.s.jdbc.core.StatementCreatorUtils : Setting SQL statement parameter value: column index 6, parameter value [1200], value class [java.lang.Integer],
Я пробую все. Изменить конструкторы. Используйте @PersistenceCreator. Попробуйте удалить keyColumn = "id". Но при удалении у меня появляется новая ошибка, связанная с ключом_ордеров. Выполнение подготовленного оператора SQL [INSERT INTO "products" ("order_id", "orders_key", "product_amount", "product_article", "product_name", "product_price") ЗНАЧЕНИЯ (?, ?, ?, ?, ?, ?)]. Я попробовал изменить List на Set и получил ту же ошибку
Подробнее здесь: https://stackoverflow.com/questions/792 ... -jdbc-only
Мобильная версия