Icarus
Icarus

Reputation: 511

unable to get table from postgreSQL despite the spring boot program being connected to database and the database not being empty

I am rather new to Spring boot and I am trying to write a very simple program that can perform post, get and delete on a postgreSQL data base. the database is named "recipes" schema "public" and table "recipe" The problem that I ran into is that when I make the get request through postman, it simply returns null despite the data base being initialized with data.

like so enter image description here

I did my best to try and narrow down the problem and the furthest I got is that line from the service layer is returning nothing when evaluated

jdbcTemplate.query(sql, new RecipeRowMapper())

The database is initialized with the following SQL

INSERT INTO recipe(id, name, ingredients, instructions, date_added)
values (1, 'ini test1', '10 cows 20 rabbits', 'cook ingredients with salt', '2004-01-02'),
       (2, 'ini test2', '30 apples 20 pears', 'peel then boil', '2004-01-13');

I know the database is not empty because when I run the following SQL

SELECT * from recipe

i get

enter image description here

And the data base is connected as seen below (one thing I do find strange is that the table "recipe" isn't showing up in the DB browser but I don't know what to make of it)

enter image description here

application.yml

app:
  datasource:
    main:
      driver-class-name: org.postgresql.Driver
      jdbc-url: jdbc:postgresql://localhost:5432/recipes?currentSchema=public
      username: postgres
      password: password

server:
  error:
    include-binding-errors: always
    include-message: always

spring.jpa:
  database: POSTGRESQL
  hibernate.ddl-auto: create
  show-sql: true
  dialect: org.hibernate.dialect.PostgreSQL9Dialect
  format_sql: true

spring.flyway:
  baseline-on-migrate: true

this is the service layer

    public List<Recipe> getRecipes(){
        var sql = """
            SELECT id, name, ingredients, instructions, date_added
            FROM public.recipe
            LIMIT 50
            """;
        return jdbcTemplate.query(sql, new RecipeRowMapper());
    }

and this is the controller

    @GetMapping(path = "/test")
    public String testRecipe(){
        return recipeService.test();
    }

and rowmapper

public class RecipeRowMapper implements RowMapper<Recipe> {
    @Override
    public Recipe mapRow(ResultSet rs, int rowNum) throws SQLException {
        return new Recipe(
                rs.getLong("id"),
                rs.getString("name"),
                rs.getString("ingredients"),
                rs.getString("instructions"),
                LocalDate.parse(rs.getString("date_added"))
        );
    }
}

finally recipe entity looks like this

@Data
@Entity
@Table
public class Recipe {
    @Id
    @GeneratedValue(
            strategy = GenerationType.IDENTITY
    )
    @Column(name = "id", updatable = false, nullable = false)
    private long id;
    @Column(name = "name")
    private String name;
    @Column(name = "ingredients")
    private String ingredients;
    @Column(name = "instructions")
    private String instructions;
    @Column(name = "date_added")
    private LocalDate dateAdded;

    public Recipe(){};
    public Recipe(long id, String name, String ingredients, String instructions, LocalDate date){}

    public Recipe(String name,
                  String ingredients,
                  String instructions,
                  LocalDate dateAdded
                  ) {
        this.name = name;
        this.ingredients = ingredients;
        this.instructions = instructions;
        this.dateAdded = dateAdded;
    }
}

Upvotes: 1

Views: 471

Answers (1)

Icarus
Icarus

Reputation: 511

As it turns out the problem is caused by the LocalDate not being converted correctly and was being posted as null. That caused the

 LocalDate.parse(rs.getString("date_added"))

to throw a null pointer exception which is what has been causing all the problems...

Upvotes: 1

Related Questions