timescaledb with spring data jpa

Viewed 707

It's super simple to crank up a little service that persists to timescaledb in spring data. But while spring data will connect and create your schema from your model, it obviously doesn't create the hypertables that wrap your tables. What is the standard way to create your hypertables with a spring boot service using spring data?

1 Answers

Would something like this work?

@Slf4j
@Repository
@DependsOn({"readingRepository"})
public class CustomTimescaleRepository {
    @PersistenceContext
    EntityManager entityManager;

    @PostConstruct
    @Transactional
    @Modifying
    void createHypertables() {
        log.info("CREATING HYPERTABLES");
        Query query = entityManager.createNativeQuery("SELECT create_hypertable('reading','timestamp')");
        query.getFirstResult();
    }
}
Related