I am attempting to develop a simple Spring Boot application for managing coupons, to create and read coupons.The application runs without issues when started.
However, upon sending a POST request via Postman, I encounter the following error in application :
java.sql.SQLSyntaxErrorException: Table 'demo.coupon_seq' doesn't exist
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:121) ~[mysql-connector-j-8.3.0.jar:8.3.0]
In error it is saying ‘demo.coupon_seq’ doesn’t exist but i created class ‘coupon’ i dont know why it is looking for coupon_seq ?
can anyone please look into this and provide solution
Model/Entity Class :
@Entity
@Table(name = "coupon")
public class Coupon {
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
private int id;
private String couponname;
private int discount;
@Column(name = "expdata")
private String expdate;
}
Controller Class :
@RestController
@RequestMapping("/coupon")
public class CouponController {
@Autowired
CouponRepository repository;
@PostMapping(value = "/createCoupon")
public Coupon addCoupon(Coupon coupon) {
return repository.save(coupon);
}
}
application.properties file :
spring.application.name=springcloud
spring.datasource.url=jdbc:mysql://localhost:3306/demo
spring.datasource.username=root
spring.datasource.password=Passw0rd123
spring.jpa.show-sql=true
SQL Quries
create database demo;
use demo;
create table coupon (
id int auto_increment,
couponname varchar(20),
discount int,
expdata varchar(10),
primary key(id)
);
drop table coupon;
select *from coupon;
Postman :
3
Answers
Try using
instead of
spring.jpa.hibernate.ddl-auto=update
This property updates schema according to the entities, so it’ll create
coupon_seq
.Else you can create it by yourself.
try this