views:

269

answers:

2

Is it possible to determine the native table name of an entity?

If Table annotation is present it's easy:

entityClass.getAnnotation(Table.class).name()

But is it possible to get the table name, if no Table annotation is present?

Hibernate provides the information via Configuration https://www.hibernate.org/hib_docs/v3/api/org/hibernate/cfg/Configuration.html#getClassMapping%28java.lang.String%29 :

configuration.getClassMapping(entityClass.getSimpleName()).getTable().getName()

Is there any similar option available in JPA? I think not, or?

Thanks a lot

+1  A: 

If you use @Table annotation, there is no problem, as you have shown. If you don't use that annotation, then table name is the same as the class name (JPA default).

The fun starts if you use mapping file, you need to parse it and retrive table name - this is not very difficult, but demands some work. If you are afraid of performance issues then you can parse mapping file(s) once and cache all tables names.

Piotr Kochański
+1  A: 

If no table annotation is present (and no ORM.xml) then in JPA the table name is formed based on the class name (see the JPA spec). Hence why exactly do you need an accessor method ?

See http://www.datanucleus.org/products/accessplatform_2_0/jpa/orm/datastore_identifiers.html

DataNucleus
I wanted to avoid implementing the algorithm again. And I wanted to avoid parsing the XML mapping file, too.But I already thought, there would be no way to ask the JPA implementation for the real table names.Thanks a lot.
marabol