By default database table is mapped by the @Entity name. If element 'name' of @Entity is not specified then the entity name and the table name will be the unqualified class name otherwise it will be the 'name' of the entity. If table name is different than entity name, then we can use @Table annotation as shown in this example.
public class ExampleMain { private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory( "testPersistenceUnit"); private static final EntityManager entityManager = emf.createEntityManager();
public static void main(String[] args) { //h2 native query to show tables and columns runNativeQuery("SHOW TABLES"); runNativeQuery("SHOW COLUMNS from ExampleTable"); }
private static void runNativeQuery(String s) { System.out.println("--------\n" + s); Query query = entityManager.createNativeQuery(s); List list = query.getResultList(); for (Object o : list) { System.out.println(Arrays.toString((Object[]) o)); } } }
Output
-------- SHOW TABLES [EXAMPLETABLE, PUBLIC] -------- SHOW COLUMNS from ExampleTable [ID, VARCHAR(2147483647), NO, PRI, NULL]