提问人:Federico Wedemeyer 提问时间:11/16/2023 最后编辑:Federico Wedemeyer 更新时间:11/17/2023 访问量:37
JpaRepository.save(entity) 不起作用。SQLError 0,SQLState:null
JpaRepository.save(entity) not working. SQLError 0, SQLState: null
问:
我正在尝试使用 JPA、Lombok 和 Hibernate 在 Java Spring 上制作一个 POST 端点。
我将向你展示我在控制器、实体、服务上的代码,以及我必须查看你是否可以帮助我的错误屏幕截图。
对不起,代码上的西班牙语,这是给大学的。
具有 POST 请求的控制器:
@PostMapping
public ResponseEntity<Object> iniciarAlquiler(@RequestBody AlquilerRequest aRequest) {
try {
val alquiler = alquilerService.iniciarAlquiler(aRequest.getClienteId(), aRequest.getEstacionId());
System.out.println(alquiler);
return ResponseHandler.success(AlquilerResponse.from(alquiler));
} catch (IllegalArgumentException e) {
return ResponseHandler.badRequest(e.getMessage());
} catch (Exception e) {
return ResponseHandler.internalError();
}
}
实体:
@Entity
@Table(name = "ALQUILERES")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Alquiler {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private long id;
@Column(name = "ESTACION_DEVOLUCION")
private int estacionDevolucion;
@Column(name = "ESTACION_RETIRO")
private int estacionRetiro;
@Column(name = "ESTADO")
private int estado;
@Column(name = "FECHA_HORA_DEVOLUCION")
private LocalDateTime horaDevolucion;
@Column(name = "FECHA_HORA_RETIRO")
private LocalDateTime horaRetiro;
@Column(name = "ID_CLIENTE")
private long idCliente;
@Column(name = "MONTO")
private double monto;
@ManyToOne
@JoinColumn(name = "ID_TARIFA")
private Tarifa tarifa;
服务(注意:definirTarifa工作正常。它找到另一个实体):
@Override
public Alquiler iniciarAlquiler(int idCliente, int estacionRetiroId) {
val estado = 1;
LocalDateTime fechaHoraRetiro = LocalDateTime.now();
val tarifa = definirTarifa(fechaHoraRetiro);
val newAlquiler = new Alquiler();
newAlquiler.setEstado(estado);
newAlquiler.setEstacionRetiro(estacionRetiroId);
//newAlquiler.setHoraRetiro(fechaHoraRetiro);
newAlquiler.setIdCliente(idCliente);
newAlquiler.setTarifa(tarifa);
return alquileresRepository.save(newAlquiler);
}
我是编程的新手,事实上这是我的第一个 StackOverflow 问题。我学习这个的班级不好,我基本上必须自己学习所有东西。我几乎不知道自己在做什么。如果您也可以将我重定向到一些好的资源来学习这个或自己解决问题,我将不胜感激。
答:
0赞
Arun Duraisamy
11/16/2023
#1
JPA 2.1 是在 Java 8 之前发布的,因此不支持新的日期和时间 API。 因此,请检查 horaDevolucion 和 horaRetiro 数据类型等列,并检查 SQLite 是否支持该数据类型。
两种选择
- 更新到 JPA 2.2 和 hibernate 5 支持日期和时间 API
- 使用 @Converter 将 LocalDateTime 转换为 Timestamp。有关示例,请参阅此链接
评论
0赞
Federico Wedemeyer
11/17/2023
看起来我的所有依赖项都是最新的。我会尝试转换
评论
spring.jpa.database-platform=org.hibernate.community.dialect.SQLiteDialect spring.jpa.hibernate.ddl-auto=none spring.datasource.url=jdbc:sqlite:bicicletas.db spring.datasource.driver-class-name=org.sqlite.JDBC spring.datasource.username=admin spring.datasource.password=admin