Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the field annotated with @Id in EJB3 (JPA) and Hibernate?

The title is self explanatory.

I would be glad to hear solutions, thanks.

like image 663
javatar Avatar asked Dec 06 '22 19:12

javatar


2 Answers

There's a shorter approach than listed so far:

Reflections r = new Reflections(this.getClass().getPackage().getName());
Set<Field> fields = r.getFieldsAnnotatedWith(Id.class);
like image 157
KendallV Avatar answered Apr 19 '23 07:04

KendallV


JPA2 has a metamodel. Just use that, and you then stay standards compliant. The docs of any JPA implementation ought to give you enough information on how to access the metamodel

like image 45
DataNucleus Avatar answered Apr 19 '23 07:04

DataNucleus