Reputation: 213
I've a spring boot project, and I'm using hibernate to map my entities to DB, However now I have a new requirement: I need to be able to dynamically create tables in DB, without any mapping (so far).
Does anyone know about some framework, to help me to deal with this: -I want to execute SQL or similar(ddl) to create my tables -I need to deal with connection management
I've been told about spring-data, but I need some opinion about this.
An example: Imagine I have a service available to client, for example:
class DBHACKService {
void executeSQL(String ddl).
}
The client invoke this like :
new DBHackService.executeSQL ("create table mytable (name varchar)");
In this method I can do some manipulation to the sql. The question is: which is the best way to send it to DBEngine.
Thanks again
Thanks in advance. rui
Upvotes: 2
Views: 7466
Reputation: 761
What do you wish to do with the sql passed as parameters.
If you wish to validate the sql, one option would be to fire query and if you get an exception which implies there is some issue with passed argument.
If you wish to parse sql query then you can use jSqlParser. http://jsqlparser.sourceforge.net/
Upvotes: -1
Reputation: 3162
You can have a look at DdlUtils where one can feed it at any time with the changes you need at runtime and have your changes reflected on the DB. The format is database-independent so you won't have to worry about portability yourself.
I do not know how up to date it is though.
If you fancy Groovy.
Upvotes: 2
Reputation: 116051
Take a look at the Spring Boot documentation on database initialization.
The simplest option is to place a schema.sql
file in the root of the classpath (for example in src/main/resources
). Any DDL in this file will be run as part of your application's startup. One of Spring Boot's JDBC samples shows this in action.
A second, more sophisticated, option is to use a database migration tool like Liquibase or Flyway. The advantage of a migration tool is that it makes it much easier to evolve your database schema as your application's needs change. Spring Boot has small sample applications for both Liquibase and Flyway.
Upvotes: 1
Reputation: 761
If you wish to create schema dynamically I guess this should help you.
Hibernate: Automatically creating/updating the db tables based on entity classes
You could issue ddl statments using jdbc to create tables on the fly. Keeping the queries in some xml file. So that it remains generic. Have a watcher service on the config file, the moment file gets updated with new query you can fire required jdbc calls
Upvotes: 0