How to know if a class is an @Entity (javax.persistence.Entity)?

江枫思渺然 提交于 2021-02-10 18:17:22

问题


How can I know if a class is annotated with javax.persistence.Entity?

Person (Entity)

@Entity
@Table(name = "t_person")
public class Person {
...
}

PersonManager

@Stateless
public class PersonManager {

    @PersistenceContext
    protected EntityManager em;

    public Person findById(int id) {
        Person person = this.em.find(Person.class, id);
        return person;
    }

I try to do it with instance of as the following

@Inject
PersonManager manager;

Object o = manager.findById(1);
o instanceof Entity  // false

however the result is false, shouldn't it be true?


回答1:


@NiVer's answer is valid. But, if you don't have a session or sessionFactory at that point you could use Reflection. Something like:

o.getClass().getAnnotation(Entity.class) != null;



回答2:


While the existing answers provide a (somehow) working solution, some things should be noted:

  1. Using an approach based on Reflection implies (a) Performance Overhead and (b) Security Restrictions (see Drawbacks of Reflection).

  2. Using an ORM-specific (here: Hibernate) approach risks portability of the code towards other execution environments, i.e., application containers or other customer-related settings.

Luckily, there is a third JPA-only way of detecting whether a certain Java class (type) is a (managed) @Entity. This approach makes use of standardized access to the javax.persistence.metamodel.MetaModel. With it you get the method

Set < EntityType > getEntities();

It only lists types annotated with @Entity AND which are detected by the current instance of EntityManager you use. With every object of EntityType it is possible to call

Class< ? > getJavaType();

For demonstration purposes, I quickly wrote a method which requires an instance of EntityManager (here: em), either injected or created ad-hoc:

private boolean isEntity(Class<?> clazz) {
    boolean foundEntity = false;
    Set<EntityType<?>> entities = em.getMetamodel().getEntities();
    for(EntityType<?> entityType :entities) {
        Class<?> entityClass = entityType.getJavaType();
        if(entityClass.equals(clazz)) {
            foundEntity = true;
        }
    }
    return foundEntity;
}

You can provide such a method (either public or protected) in a central place (such as a Service class) for easy re-use by your application components. The above example shall just give a direction of what to look for aiming at a pure JPA approach.

For reference see sections 5.1.1 (page 218) and 5.1.2 (page 219f) of the JPA 2.1 specification.

Hope it helps.




回答3:


If the statement

sessionFactory.getClassMetadata( HibernateProxyHelper.getClassWithoutInitializingProxy( Person.class ) ) != null;

is true, than it is an entity.



来源:https://stackoverflow.com/questions/49307727/how-to-know-if-a-class-is-an-entity-javax-persistence-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!