@PostConstruct populate DB at startup - Spring Boot 2.2.4 upgrade

Viewed 313

I'm upgrading my application from Spring Boot 1.5 to 2.2. Now my db inits aren't working.

The culprit in my case seems to be the HibernateTransactionManager when using this my @PostConstruct or @EventListener(ApplicationReadyEvent.class)method isn't saving to db, even though the code is running. When removing the transactionManager bean the @PostConstruct code works fine. But I have other code that depend on this bean in my project, so I need it.

Also if I make the same code a rest endpoint and run it manually it always works.

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
    return new HibernateTransactionManager(emf.unwrap(SessionFactory.class));
}
@EventListener(ApplicationReadyEvent.class)
public void initDateRanges(){
    // Check if date ranges already present
    if (!jsonBucketRepo.findById(DateRangeDto.ID).isPresent()) {
        List<DateRangeDto> dateRangeDtoList = new ArrayList<>(5);
        dateRangeDtoList.add(new DateRangeDto("TODAY", "moment().startOf('day').format(dateFormat)", "moment().endOf('day').format(dateFormat)"));
        dateRangeDtoList.add(new DateRangeDto("YESTERDAY", "moment().subtract(1, 'days').startOf('day').format(dateFormat)", "moment().subtract(1, 'days').endOf('day').format(dateFormat)"));
        dateRangeDtoList.add(new DateRangeDto("THIS_WEEK", "moment().startOf('isoweek').format(dateFormat)", "moment().endOf('isoweek').format(dateFormat)"));
        dateRangeDtoList.add(new DateRangeDto("THIS_MONTH", "moment().startOf('month').format(dateFormat)", "moment().endOf('month').format(dateFormat)"));
        dateRangeDtoList.add(new DateRangeDto("LAST_MONTH", "moment().subtract(1, 'months').startOf('month').format(dateFormat)", "moment().subtract(1, 'months').endOf('month').format(dateFormat)"));
        JsonBucket rangeJson = DateRangeDto.dtoList2NewJsonBucket(dateRangeDtoList);
        JsonBucket bucket = jsonBucketRepo.save(rangeJson);
    }
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonPropertyOrder({"nameOfRange", "fromMomentSyntax", "toMomentSyntax"})
public class DateRangeDto {

    public static final String ID = "daterange";
    public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().findAndRegisterModules();

    private String nameOfRange;
    private String fromMomentSyntax;
    private String toMomentSyntax;

    public static List<DateRangeDto> jsonBucket2DtoList(JsonBucket result) throws IOException {
        if(!result.id.equals(DateRangeDto.ID)){
            throw new IllegalArgumentException(String.format("JsonBucket has to have ID '%s'", DateRangeDto.ID));
        }
        ObjectReader reader = DateRangeDto.OBJECT_MAPPER.readerFor(new TypeReference<List<DateRangeDto>>() {});
        List<DateRangeDto> list = reader.readValue(result.getJsonNode());
        return list;
    }

    public static JsonBucket dtoList2NewJsonBucket(List<DateRangeDto> rangeDtoList) {
        JsonNode jsonNode = DateRangeDto.OBJECT_MAPPER.valueToTree(rangeDtoList);
        JsonBucket jsonBucket = new JsonBucket(DateRangeDto.ID);
        jsonBucket.setJsonNode(jsonNode);
        return jsonBucket;
    }

    public static JsonNode dtoList2JsonNode(List<DateRangeDto> rangeDtoList) {
        return DateRangeDto.OBJECT_MAPPER.valueToTree(rangeDtoList);
    }
}
@Entity
@Table(name = "tt_json_bucket")
@Data
@NoArgsConstructor
@TypeDef(
        name = "json-node",
        typeClass = JsonNodeStringType.class
)
public class JsonBucket {

    @Id
    String id;

    @Version
    Integer version;

    @Type(type = "json-node")
    @Column(columnDefinition = "VARCHAR(2000)")
    JsonNode jsonNode;

    public JsonBucket(String id) {
        this.id = id;
    }
}
@Repository
public interface JsonBucketRepository extends CrudRepository<JsonBucket, String> {

}
0 Answers
Related