java 加载 javassist-ed Hibernate 实体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1139611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:20:17  来源:igfitidea点击:

Loading javassist-ed Hibernate entity

javahibernatejpa

提问by Damo

I have a JSF converter that I use for a SelectItem list containing several different entity types. In the getAsString()method I create the string as the class name suffixed with ":" and the ID.

我有一个 JSF 转换器,用于包含多种不同实体类型的 SelectItem 列表。在该getAsString()方法中,我将字符串创建为以“:”和 ID 为后缀的类名。

MySuperClass superClass = (MySuperClass)value;
if(superClass != null) {
  return String.valueOf(superClass.getClass().getName()+":"+superClass.getId());
}

This allows me to load the correct entity in the getAsObject()on the way back from the UI by doing this :

这允许我getAsObject()通过执行以下操作在从 UI 返回的途中加载正确的实体:

String className = value.substring(0, value.indexOf(":"));
long id = Long.parseLong(value.substring(value.indexOf(":")+1));
Class<T> entitySuperClass = (Class<T>) Class.forName(className);
MySuperClass superClass = (MySuperClass)getEntityManager().find(entitySuperClass, id);

My problem is that my entity in getAsString()is a proxy. So instead of getting com.company.MyEntitywhen I do a getClass().getName() I am getting com.company.MyEntity_$$_javassist_48so then it fails on the find().

我的问题是我的实体getAsString()是代理。因此,com.company.MyEntity当我执行 getClass().getName() 时,我得到的不是得到com.company.MyEntity_$$_javassist_48,而是在find().

Is there any way (aside from String manipulation) to get the concrete class name (eg. com.company.MyEntity)?

有没有办法(除了字符串操作)来获取具体的类名(例如 com.company.MyEntity)?

Thanks.

谢谢。

回答by ChssPly76

Instead of superClass.getClass()try org.hibernate.proxy.HibernateProxyHelper.getClassWithoutInitializingProxy(superClass).

而不是superClass.getClass()尝试org.hibernate.proxy.HibernateProxyHelper.getClassWithoutInitializingProxy(superClass)

回答by Daniel Bleisteiner

There is one important difference between Hibernate.getClass()and HibernateProxyHelper! The HibernateProxyHelperalways returns the superclass that represents the table in the database if you have and entity that is mapped using

Hibernate.getClass()HibernateProxyHelper之间有一个重要区别!该HibernateProxyHelper总是返回使用映射表示在数据库中的表,如果你有超类和实体

@Table(name = SuperClass.TABLE_NAME)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = SuperClass.TABLE_DISCRIMINATOR, discriminatorType = DiscriminatorType.STRING)

and

@DiscriminatorValue(value = EntityClass.TABLE_DISCRIMINATOR)

in the subclass.

在子类中。

Hibernate.getClass(...)returns the real subclass for those.

Hibernate.getClass(...)返回那些真正的子类。

回答by Asaf

When combined with abstract entity's inheritance (AbstractEntity<- ConcreteEntity<- ConcreteEntityProxy), getting the persistence class is just not enough:

当结合抽象实体的继承(AbstractEntity<- ConcreteEntity<- ConcreteEntityProxy)时,获得持久性类是不够的:

// This should fail - trying to create an abstract class
HibernateProxyHelper.getClassWithoutInitializingProxy(superClass).newInstance()

instead get the implementation class:

而是获取实现类:

protected <T> T deproxy(T maybeProxy) {
    if (maybeProxy instanceof HibernateProxy) {
        return (T) ((HibernateProxy) maybeProxy).getHibernateLazyInitializer().getImplementation();
    }
    return maybeProxy;
}