gpt4 book ai didi

java - 如何修复 Spring Boot 应用程序中 SimpleJdbcInsert 中的错误

转载 作者:行者123 更新时间:2023-12-02 10:06:18 27 4
gpt4 key购买 nike

我学习Spring Boot阅读《Spring in Action 5》一书。我尝试向H2嵌入式数据库插入数据。我使用SimpleJdbcInsert和executeAndReturnKey方法。

这是一个构造函数:

 @Autowired
public JdbcOrderRepository(JdbcTemplate jdbc) {

this.orderInserter = new SimpleJdbcInsert(jdbc).withTableName("Taco_Order").usingGeneratedKeyColumns("id");
this.orderTacoInserter = new SimpleJdbcInsert(jdbc).withTableName("Taco_Order_Tacos");
this.objectMapper = new ObjectMapper();
}

以下是插入数据的方法:

@Override
public Order save(Order order) {

order.setPlacedAt(new Date());
long orderId = saveOrderDetails(order);
order.setId(orderId);
List<Taco> tacos = order.getTacos();

for (Taco taco : tacos)
saveTacoToOrder(taco, orderId);

return order;
}

private long saveOrderDetails(Order order) {
Map<String, Object> values = objectMapper.convertValue(order, Map.class);
values.put("placedAt", order.getPlacedAt());
long orderId = orderInserter.executeAndReturnKey(values).longValue();

return orderId;
}

错误出现在这个字符串上:

long orderId = orderInserter.executeAndReturnKey(values).longValue();

错误文本:

 There was an unexpected error (type=Internal Server Error, status=500).
PreparedStatementCallback; ???????? NULL ?? ????????? ??? ????
"DELIVERYNAME" NULL not allowed for column "DELIVERYNAME"; SQL statement:
INSERT INTO Taco_Order (DELIVERYNAME, DELIVERYSTREET, DELIVERYCITY,
DELIVERYSTATE, DELIVERYZIP, CCNUMBER, CCEXPIRATION, CCCVV, PLACEDAT)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?) [23502-197]; nested exception is
org.h2.jdbc.JdbcSQLException: ???????? NULL ?? ????????? ??? ????
"DELIVERYNAME" NULL not allowed for column "DELIVERYNAME"; SQL statement:
INSERT INTO Taco_Order (DELIVERYNAME, DELIVERYSTREET, DELIVERYCITY,
DELIVERYSTATE, DELIVERYZIP, CCNUMBER, CCEXPIRATION, CCCVV, PLACEDAT)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?) [23502-197]

Bur 在调试器中一切正常:value 参数完全由值加载。 idea screensot

类(class)顺序:

@Data
public class Order {

@NotBlank(message = "Name is required")
private String name;

@NotBlank(message = "Name is required")
private String street;

@NotBlank(message = "Name is required")
private String city;

@NotBlank(message = "Name is required")
private String state;

@NotBlank(message = "Name is required")
private String zip;

@CreditCardNumber(message = "Not valid cc")
private String ccNumber;

@Pattern(regexp = "^(0[1-9]|1[0-2])([\\/])([1-9][0-9])$", message = "Must be formatted MM/YY")
private String ccExpiration;

@Digits(integer = 3, fraction = 0, message = "Invalid CVV")
private String ccCVV;

private Long id;
private Date placedAt;

List<Taco> tacos = new ArrayList<>();

public void addDesign(Taco design) {
this.tacos.add(design);
}

}

如何解决这个问题并使用SimpleJdbcInsert将数据插入H2数据库?

最佳答案

我今天遇到了同样的问题。我能够解决这个问题。

要解决此删除问题,请转到 schema.sql(您在其中创建表的文件)并从 Taco_Order 表中的字段中删除交付。

摘自书本

SimpleJdbcInsert has a couple of useful methods for executing the insert: execute() and executeAndReturnKey(). Both accept a Map, where the map keys correspond to the column names in the table the data is inserted into. The map values are inserted into those columns.

键名称是 Order.java 中的字段

关于java - 如何修复 Spring Boot 应用程序中 SimpleJdbcInsert 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55319027/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com