Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the table name in OrmLite

Tags:

java

ormlite

In OrmLite the name of a table is either the name of the class it is based on or whatever is defined in @DatabaseTable(tableName = "...") annotating that class.

Is there a convenient way to get the name of a table at runtime? In my case I need the name of all tables when upgrading the database to recreate database triggers based on those tables.

I already checked http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/table/TableInfo.html but I don't understand what the ID is supposed to be.

like image 435
Benjamin Avatar asked Apr 24 '14 10:04

Benjamin


1 Answers

You could use the extractTableName method from the DatabaseTableConfig class.

Extract and return the table name for a class.

public class Test {  
    public static void main(String[] args){
        String name = DatabaseTableConfig.extractTableName(A.class);
        System.out.println(name); //TableA
    }
}
@DatabaseTable(tableName="TableA")
class A {}
like image 166
Alexis C. Avatar answered Oct 02 '22 02:10

Alexis C.