I have an unknown JPA entity and need to know it’s version. I could not find a way to do this generically.
I tried the metamodel, but don’t know what to pass to getVersion() method:
Object entity = ...;
Metamodel metamodel = entityManager.getMetamodel();
IdentifiableType<?> metaClass =
(IdentifiableType<?>)metamodel.managedType(entity.getClass());
metaClass.getVersion(<what to put here?>);
Same pattern is used in metaClass.getId(), but there is a complementary method getIdType() — getVersionType() is missing.
Secondly, there is entityManagerFactory.getPersistenceUnitUtil().getIdentifier(entity) method, but not getVersion(entity) method.
Is there some way to get version from an unknown entity?
I solved it in the following way: The
javax.persistence.Versionclass specifies supported datatypes, only seven are supported, so I try all of them one after other. The JPA spec apparently misses thegetVersionType()method, I think it is a bug.