Search code examples
google-app-engineormgoogle-cloud-sqlormlite

Automate OrmLite table creation


I used OrmLite to map Java objects in my Google App Engine application to a bunch of database tables (MySQL). Is there a way to automatically create the tables on Google's Cloud SQL or a similar cloud based SQL service instead of having to manually create the tables myself.

OrmLite's documentation does not cover this, neither does Google App Engine's.

Any pointer in the right direction is highly appreciated.


Solution

  • Is there a way to automatically create the tables ... OrmLite's documentation does not cover this ...

    I'm not sure if you are talking specifically about cloud SQL but ORMLite certainly has a ton of documentation about creating tables in general.

    • TableUtils is the class that supports (uh) table utility methods like create and delete. Here's the javadocs.
    • In the Getting Started section it talks about using TableUtil to create the schema in the Code Example. More details in the How To Use
    • In the documentation index I see entries for: "creating a table" and "table creation".

    To quote from the sample code from the Getting Started docs.

    // instantiate the dao
    Dao<Account, String> accountDao =
    DaoManager.createDao(connectionSource, Account.class);
    
    // if you need to create the 'accounts' table make this call
    TableUtils.createTable(connectionSource, Account.class);
    

    TableUtil also has a method that returns the SQL statements that do the create.