skip to Main Content

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 :

enter image description here

3

Answers


  1. Try using

    @GeneratedValue (strategy = GenerationType.IDENTITY)
    

    instead of

    @GeneratedValue (strategy = GenerationType.AUTO)
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. try this

    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    
    spring.jpa.hibernate.ddl-auto=update
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search