gpt4 book ai didi

class - 如何从 JPA 实体名称中获取 Class?

转载 作者:行者123 更新时间:2023-12-01 08:26:01 25 4
gpt4 key购买 nike

有没有办法查到

  java.lang.Class 

那就是拥有
@Entity(name = "X")?

换句话说,使用实体名称来获取实体的类名,当然是在运行时:)

最佳答案

所有已注册 @Entity s 可通过 MetaModel#getEntities() 获得.它返回一个 List<EntityType<?>>由此 EntityType 有一个 getName() 表示 @Entity(name) 的方法和一个 getJavaType() 代表关联的 @Entity类(class)。 MetaModel实例依次由 EntityManager#getMetaModel() 提供.

换句话说,这是实用方法的风格:

public static Class<?> getEntityClass(EntityManager entityManager, String entityName) {
for (EntityType<?> entity : entityManager.getMetamodel().getEntities()) {
if (entityName.equals(entity.getName())) {
return entity.getJavaType();
}
}

return null;
}

关于class - 如何从 JPA 实体名称中获取 Class?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32910944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com