15

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?

rjdkolb
  • 10,377
  • 11
  • 69
  • 89
niklassaers
  • 8,480
  • 20
  • 99
  • 146

2 Answers2

31

Easy enough using general reflection:

import javax.persistence.Table;

.....

Class<?> c = User.class;
Table table = c.getAnnotation(Table.class);
String tableName = table.name();
skaffman
  • 398,947
  • 96
  • 818
  • 769
8

Similar to Get the table name from the model in Hibernate

Table table = Entity.class.getAnnotation(Table.class);
String tableName = table.name();
Community
  • 1
  • 1
n002213f
  • 7,805
  • 13
  • 69
  • 105