Is there a way to find out the
java.lang.Class
that is the one having
@Entity(name = "X")?
In other words use the Entity Name to get the classname of the Entity, of course at runtime :)
All registered @Entitys are available by MetaModel#getEntities(). It returns a List<EntityType<?>> whereby the EntityType has a getName() method representing the @Entity(name) and a getJavaType() representing the associated @Entity class. The MetaModel instance is in turn available by EntityManager#getMetaModel().
In other words, here's the approach in flavor of an utility method:
public static Class<?> getEntityClass(EntityManager entityManager, String entityName) {
for (EntityType<?> entity : entityManager.getMetamodel().getEntities()) {
if (entityName.equals(entity.getName())) {
return entity.getJavaType();
}
}
return null;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With