Rest api response data not saved in the database using Spring JPA

Viewed 1060

I'm new with anything Spring related, so would you please help me with this? I'm trying to write a registration api that saves some data in the database using Spring JPA.

Here is my code:

@Entity model:

@Entity
@Table(name = "registerapplications")
public class RegisterApplication {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id; 

    private String applicationId;

    public RegisterApplication (String applicationId) {
        this.applicationId = applicationId;
    }

    // Getters and Setter
}

Repository:

public interface RegisterRepository extends CrudRepository<RegisterApplication, Long>{

}

Controller:

@RestController
public class RegisterApplicationController {

    @Autowired
    private RegisterRepository registerRepository;

    @RequestMapping (path="/register", method=RequestMethod.POST)
    public @ResponseBody RegisterApplication registerApplicationController() {
        RegisterApplication registerApplication = new RegisterApplication(UUID.randomUUID().toString());
        registerRepository.save(registerApplication);
        return registerApplication;
    }   
}

application.properties:

spring.jpa.hibernate.ddl-auto=create
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/register
spring.datasource.username=admin
spring.datasource.password=admin

The request works properly, but it does not save the data in my table: Database screenshot

I get no error, so I don't know where the problem is. Would you please take a look on the code and give me some hints?

1 Answers

I think the reason for your problem is that you forgot to annotate the applicationId field in your entity with @Column.

Related