如何知道一个类是否为@Entity(javax.persistence.Entity)? [英] How to know if a class is an @Entity (javax.persistence.Entity)?

查看:95
本文介绍了如何知道一个类是否为@Entity(javax.persistence.Entity)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么知道一个类是否用javax.persistence.Entity注释?

How can I know if a class is annotated with javax.persistence.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

结果是false,不是吗true?

推荐答案

虽然现有答案提供了(某种程度上)可行的解决方案,但应注意以下几点:

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

  1. 使用基于反射的方法意味着(a)性能开销和(b)安全限制(请参见使用特定于ORM的方法(在此为Hibernate)会导致代码向其他执行环境(即应用程序容器或其他与客户相关的设置)的可移植性.

    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.

    幸运的是,还有第三种 JPA 专用的方法来检测某个Java类(类型)是否是(托管)@Entity.此方法利用对javax.persistence.metamodel.MetaModel的标准化访问.有了它,您将获得方法

    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

    设置< EntityType> getEntities();

    Set < EntityType > getEntities();

    它仅列出使用@Entity AND注释的类型,这些类型由您使用的EntityManager的当前实例检测到.对于EntityType的每个对象,可以调用

    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();

    Class< ? > getJavaType();

    出于演示目的,我快速编写了一个方法,该方法需要一个EntityManager实例(此处为em),该实例可以是临时注入的或临时创建的:

    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;
    }
    

    您可以在中央位置(例如Service类)提供这种方法(公共的或受保护的),以方便应用程序组件重复使用.上面的示例仅给出了针对纯粹的 JPA 方法的寻找方向.

    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.

    作为参考,请参阅希望有帮助.

    这篇关于如何知道一个类是否为@Entity(javax.persistence.Entity)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆