views:

262

answers:

2

Hi guys, 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?

Cheers

Nik

+5  A: 

Easy enough using general reflection:

import javax.persistence.Table;

.....

Class<?> c = User.class;
Table table = c.getAnnotation(Table.class);
String tableName = table.name();
skaffman
Thanks a lot! Wow, you guys really are fast! :-)
niklassaers
Hmm.... I couldn't find name() in org.hibernate.annotations.Table. Am I looking at the right table class?
niklassaers
No, you're looking at the Hibernate-propriety extensions to JPA. You need javax.persistence.Table.
skaffman
Aha! Thanks a lot :-)
niklassaers
+1  A: 

Similar to http://stackoverflow.com/questions/634342/get-the-table-name-from-the-model-in-hibernate

Table table = Entity.class.getAnnotation(Table.class);
String tableName = table.name();
n002213f
Thanks a lot! Wow, you guys really are fast! :-)
niklassaers