Get annotated hibernate tablename from POJO

niklassaers picture niklassaers · Aug 24, 2009 · Viewed 16.6k times · Source

I have an entity which is declared roughly like:

@Entity
@Table(name = "myUserTable")
public class User implements Serializable { ... }

I'm making a generic DAO class, and in doing so I'd like to retrieve the "myUserTable" name. Is there any way I can reach this name?

Answer

skaffman picture skaffman · Aug 24, 2009

Easy enough using general reflection:

import javax.persistence.Table;

.....

Class<?> c = User.class;
Table table = c.getAnnotation(Table.class);
String tableName = table.name();