Careeroza
Seeding Data
data.sql and CommandLineRunner
For local development you often want a few products in the database at startup. Two common ways:
-- src/main/resources/data.sql (runs after Hibernate creates the tables
-- when spring.jpa.defer-datasource-initialization=true)
INSERT INTO products (sku, name, description, price, stock, category) VALUES
('ELE-1001', 'Wireless Mouse', '2.4 GHz, silent clicks', 799.00, 120, 'Electronics'),
('BOO-2001', 'Clean Code', 'Robert C. Martin, paperback', 450.00, 35, 'Books');@Configuration
@Profile("dev")
class DevData {
@Bean
CommandLineRunner seed(ProductRepository repo) {
return args -> {
if (repo.count() == 0) {
repo.save(new Product("ELE-1001", "Wireless Mouse", "2.4 GHz, silent clicks",
new BigDecimal("799.00"), 120, "Electronics"));
repo.save(new Product("BOO-2001", "Clean Code", "Robert C. Martin, paperback",
new BigDecimal("450.00"), 35, "Books"));
}
};
}
}Note: For real schema changes in production use Flyway or Liquibase migrations instead (Hibernate & JPA material, advanced level).