There is a functionality for embedded databases (like H2) which provides us with a possibility to automatically generate sql tables using a predefined scrypt file.
Bean example:
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
return builder
.setType(EmbeddedDatabaseType.H2)
.addScript("embeddedDB.sql")
.build();
}
Scrypt file example:
CREATE TABLE Users
(
id INT(11) NOT NULL AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
photo BLOB,
registration_date DATE NOT NULL,
role VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
);
Some time ago a saw on the web a similar solution to configure a bean that automatically generates SQL tables (when spring context starts) in case of MySQL data source. That solution didn't include JPA/Hibernate. Unfortunately, I can't find this example anymore... So, my question is how to implement automatical SQL tables generation by Spring in case of MySQL data source?