为什么Spring Data JPA SQL在插入我的数据时出错



有3个实体,客户、账户和交易

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Account {
@Id
private Long id;
}
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Customer {
@Id
private Long id;
}
@Entity
@Data
@NoArgsConstructor
public class Transaction {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne(
cascade = CascadeType.ALL
)
private Account accountNumber;
private Double amount;
private String desc;
@Temporal(TemporalType.DATE)
private Date date;
@Temporal(TemporalType.TIME)
private Date time;
@ManyToOne(
cascade = CascadeType.ALL
)
private Customer customer;
public Transaction(Long accountNumber, Double amount, String desc, String date, String time, Long customer) throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm:ss");
this.accountNumber = new Account(accountNumber);
this.amount = amount;
this.desc = desc;
this.date = dateFormat.parse(date);
this.time = timeFormat.parse(time);
this.customer = new Customer(customer);
}
}

我想插入如下数据,我使用主进行测试

@SpringBootApplication
public class ETellerApplication {
public static void main(String[] args) {
SpringApplication.run(ETellerApplication.class, args);
}
@Bean
PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Bean
CommandLineRunner run(TransactionService transactionService){
return args -> {

transactionService.saveTransaction(new Transaction(8872838283L,
123.00,
"FUND TRANSFER",
"2019-09-12",
"11:11:11",
222L));
};
}

}

我运行它时遇到这个错误。

2022-04-04 16:59:02.598  WARN 24444 --- [  restartedMain] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1064, SQLState: 42000
2022-04-04 16:59:02.598 ERROR 24444 --- [  restartedMain] o.h.engine.jdbc.spi.SqlExceptionHelper   : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc, time, id) values (8872838283, 123.0, 222, '2019-01-12', 'FUND TRANSFER', '' at line 1

上面的错误说明了SQL错误,但我无法理解真正的错误我无法处理这里发生的事情。我是Spring Boot的初学者,我做了Spring Boot教程,但这还不够。请耐心等待

使用DESC作为字段名是我的错误。我把它改成了描述。Desc是SQL 中的reserverd关键字

最新更新