Home > database >  Integrating H2 Database with Spring Boot
Integrating H2 Database with Spring Boot

Time:11-20

I`m trying to integrate H2 database into my current project. But looks like my data isn't saved into the database.

I have post request method here:

@PostMapping(value = "/api/code/new", produces = "application/json")
    public ResponseEntity<?> postCodeSnippet(@RequestBody Code rqstCode) {
        Code nextCode = new Code(idCounter  , rqstCode.getCode(), LoadDate.getLoadDate());
        System.out.println(nextCode.getId());
        codeList.add(nextCode);
        codeService.saveOrUpdate(rqstCode);
        return ResponseEntity.ok(new DTOCode(String.valueOf(nextCode.getId())));
    }

where is codeService.saveOrUpdate() should send it to my database, but didn't. My database is always empty. But other parts of the program work pretty well.

Here is my Code.java class:


import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.*;

@Entity
public class Code {

    @JsonIgnore
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String code;
    private String date;

    public Code() {
    }

    public Code(Long id, String code, String date) {
        this.id = id;
        this.code = code;
        this.date = date;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }
}

Here is my CodeRepository.java interface:


import org.springframework.data.repository.CrudRepository;

public interface CodeRepository extends CrudRepository<Code, Long> {

} 

Here is my CodeService.java class


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Period;
import java.util.List;

@Service
public class CodeService {

    @Autowired
    CodeRepository codeRepository;

    public Code getCodeById(Long id) {
        return codeRepository.findById(id).get();
    }

    public void saveOrUpdate(Code code) {
        codeRepository.save(code);
    }

    public void delete(Long id) {
        codeRepository.deleteById(id);
    }

}

application.properties

server.port=8889
management.endpoints.web.exposure.include=*
management.endpoint.shutdown.enabled=true
spring.datasource.url=jdbc:h2:file:../snippets
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update

spring.h2.console.enabled=true
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false

CodePudding user response:

After SELECT * FROM CODE, I got all of my data.

  • Related