Method to obtain the ID (aka PK) field(s) of a class

Hello,

I need to create a function like getIdFields(...): SingularAttribute[], that works for the case where I have one or several ID fields.

Using the metamodel, I observed this:

  • if the class has a single ID => EntityType.getIdType().
  • we have a class w/ 2 ID fields, but w/ a PK class (configured w/ @IdClass) => `EntityType.getIdClassAttributes()

So far so good. However, I suspect that .getIdClassAttributes() works only if there is an ID class. And would return nothing for the case where an ID class doesn’t exist. In this case, the only method is to iterate on all attributes, and inspect .isId()? E.g. as proposed here. However, in this case, I will need to add a cache, because every call would imply an iteration on the attributes.

Thanks in advance!

Cristian

However, I suspect that .getIdClassAttributes() works only if there is an ID class. And would return nothing for the case where an ID class doesn’t exist. In this case, the only method is to iterate on all attributes, and inspect .isId() ? E.g. as proposed here. However, in this case, I will need to add a cache, because every call would imply an iteration on the attributes.

That’s correct.

1 Like