Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

timescaledb with spring data jpa

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?

like image 346
Arlo Guthrie Avatar asked Nov 17 '25 09:11

Arlo Guthrie


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();
    }
}
like image 111
Arlo Guthrie Avatar answered Nov 19 '25 10:11

Arlo Guthrie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!