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

7

5

The title is self explanatory.

I would be glad to hear solutions, thanks.

Corner answered 4/10, 2011 at 11:58 Comment(2)
do you need the name, or the value is also fine?Unifoliolate
Actually I need the name itself, not the value.Corner
C
4

There's a shorter approach than listed so far:

Reflections r = new Reflections(this.getClass().getPackage().getName());
Set<Field> fields = r.getFieldsAnnotatedWith(Id.class);
Carpic answered 27/6, 2013 at 10:59 Comment(0)
L
4

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

Lovelace answered 4/10, 2011 at 14:42 Comment(3)
Hi, I use spring3 with hibernate3 and this combination does not support jpa2 yet, as far as I know.Corner
Hibernate 3.6 obviously supports JPA2. Spring alsoLovelace
Here is the answer for JPA 2.0 https://mcmap.net/q/962314/-jpa-get-id-of-entity-objectDecompress
C
4

There's a shorter approach than listed so far:

Reflections r = new Reflections(this.getClass().getPackage().getName());
Set<Field> fields = r.getFieldsAnnotatedWith(Id.class);
Carpic answered 27/6, 2013 at 10:59 Comment(0)
C
3

I'm not a java programmer, nor a user of Hibernate annotations... but I probably can still help.

This information is available in the meta data. You can get them from the session factory. I looks like this:

ClassMetadata classMetadata = getSessionFactory().getClassMetadata(myClass);
string identifierPropertyName = classMetadata.getIdentifierPropertyName();

I found this API documentation.

Chauffer answered 4/10, 2011 at 12:35 Comment(0)
C
2

I extended this answer: How to get annotations of a member variable?

Try this:

String findIdField(Class cls) {
    for(Field field : cls.getDeclaredFields()){
        Class type = field.getType();
        String name = field.getName();
        Annotation[] annotations = field.getDeclaredAnnotations();
        for (int i = 0; i < annotations.length; i++) {
            if (annotations[i].annotationType().equals(Id.class)) {
                return name;
            }
        }
    }
    return null;
}
Crimson answered 4/10, 2011 at 12:42 Comment(0)
L
2

A bit late to the party, but if you happen to know your entities have only one @Id annotation and know the type of the id (Integer in this case), you can do this :

Metamodel metamodel = session.getEntityManagerFactory().getMetamodel();
String idFieldName = metamodel.entity(myClass)
    .getId(Integer.class)
    .getName();
Lozier answered 14/5, 2018 at 13:53 Comment(0)
S
1

ClassMetadata.getIdentifierPropertyName() returns null if the entity has a composite-id with embedded key-many-to-one. So this method does not cover those situations.

Sclerous answered 18/6, 2012 at 13:39 Comment(0)
J
1

This is working for EclipseLink 2.6.0, but I don't expect any differences in Hibernate space:

  String idPropertyName;
  for (SingularAttribute sa : entityManager.getMetamodel().entity(entityClassJpa).getSingularAttributes())
     if (sa.isId()) {
        Preconditions.checkState(idPropertyName == null, "Single @Id expected");
        idPropertyName = sa.getName();
     }
Jovial answered 10/11, 2015 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.