Home > OS >  How can I change profile from HSQLDB to POSTGRES since hsqldb can't work with JAva 8 Time API?
How can I change profile from HSQLDB to POSTGRES since hsqldb can't work with JAva 8 Time API?

Time:03-08

I have a class that executes methods and there was such a problem that when changing the profile, I realized that hsqldb is not able to work with Java 8 Time API, so I want to use Profile to share execution with Postgres, I would like to know how this can be do This is class with methods

 @Repository
 public class JdbcMealRepository implements MealRepository {

private static final RowMapper<Meal> ROW_MAPPER = BeanPropertyRowMapper.newInstance(Meal.class);

private final JdbcTemplate jdbcTemplate;

private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;

private final SimpleJdbcInsert insertMeal;

@Autowired
public JdbcMealRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
    this.insertMeal = new SimpleJdbcInsert(jdbcTemplate)
            .withTableName("meals")
            .usingGeneratedKeyColumns("id");

    this.jdbcTemplate = jdbcTemplate;
    this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
}

@Override
public Meal save(Meal meal, int userId) {
    MapSqlParameterSource map = new MapSqlParameterSource()
            .addValue("id", meal.getId())
            .addValue("description", meal.getDescription())
            .addValue("calories", meal.getCalories())
            .addValue("date_time", meal.getDateTime())
            .addValue("user_id", userId);

    if (meal.isNew()) {
        Number newId = insertMeal.executeAndReturnKey(map);
        meal.setId(newId.intValue());
    } else {
        if (namedParameterJdbcTemplate.update(""  
                "UPDATE meals "  
                "   SET description=:description, calories=:calories, date_time=:date_time "  
                " WHERE id=:id AND user_id=:user_id", map) == 0) {
            return null;
        }
    }
    return meal;
}

@Override
public boolean delete(int id, int userId) {
    return jdbcTemplate.update("DELETE FROM meals WHERE id=? AND user_id=?", id, userId) != 0;
}

@Override
public Meal get(int id, int userId) {
    List<Meal> meals = jdbcTemplate.query(
            "SELECT * FROM meals WHERE id = ? AND user_id = ?", ROW_MAPPER, id, userId);
    return DataAccessUtils.singleResult(meals);
}

@Override
public List<Meal> getAll(int userId) {
    return jdbcTemplate.query(
            "SELECT * FROM meals WHERE user_id=? ORDER BY date_time DESC", ROW_MAPPER, userId);
}

@Override
@Profile(Profiles.POSTGRES_DB)
public List<Meal> getBetweenHalfOpen(LocalDateTime startDateTime, LocalDateTime endDateTime, int userId) {
    return jdbcTemplate.query(
            "SELECT * FROM meals WHERE user_id=?  AND date_time >=  ? AND date_time < ? ORDER BY date_time DESC",
            ROW_MAPPER, userId, startDateTime, endDateTime);
}

}

CodePudding user response:

change your application.properties per https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html and add postgresql to your maven/gradle dependencies.

or simply change from hsqldb to h2. Note that h2 supports OffsetDateTime but not ZonedDateTime at least in jdbc, I don't know all the conversions Spring would do if you tried to save a record with ZonedDateTime to a Timestamp column.

  • Related