www.91084.com

GVKun编程网logo

Type definition error: [simple type, class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor];

19

本文将带您了解关于Typedefinitionerror:[simpletype,classorg.hibernate.proxy.pojo.bytebuddy.ByteBuddyIntercepto

本文将带您了解关于Type definition error: [simple type, class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor];的新内容,另外,我们还将为您提供关于34-TypeError: BoxSizer.AddSpacer(): argument 1 has unexpected type ''tuple''、Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a ...、Grails 2.4 + Hibernate 4 +可搜索的插件= ClassNotFoundException:org.hibernate.impl.SessionFactoryImpl、HibernateProxy. Forgot to register a type adapter?的实用信息。

本文目录一览:

Type definition error: [simple type, class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor];

Type definition error: [simple type, class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor];

<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id=''created''>Tue Jul 09 09:31:20 CST 2019</div><div>There was an unexpected error (type=Internal Server Error, status=500).</div><div>Type definition error: [simple type, class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.jpadata.entity.User$HibernateProxy$W5saRQuh[&quot;hibernateLazyInitializer&quot;])</div></body></html>

这个错误的解决方案: JsonIgnoreProperties({"hibernateLazyInitializer","handler"})//添加这一行

@Entity

@Table(name = "tbl_user")//可以省略 public class User {

[@Id](https://my.oschina.net/u/3451001)@GeneratedValue(strategy = GenerationType.IDENTITY)

private Integer id;

@Column(name = "last_name",length = 50)

private String lastName;

@Column //省略的写法

private String email;

//下面这篇博客解释了原因

http://jingpin.jikexueyuan.com/article/43050.html

34-TypeError: BoxSizer.AddSpacer(): argument 1 has unexpected type ''tuple''

34-TypeError: BoxSizer.AddSpacer(): argument 1 has unexpected type ''tuple''

TypeError: BoxSizer.AddSpacer(): argument 1 has unexpected type ''tuple''
这个错误很烦,折腾了好久;

原因有二:
1.因为它现在只能有一个参数了:
请看

 

2.因为你可能使用老版的wxFormBuilder产生的,而如果用新版的这会是:
bSizer1.Add( ( 0, 0), 1, wx.EXPAND, 5 )
 新版下载链接:https://ci.appveyor.com/api/projects/jhasse/wxformbuilder-461d5/artifacts/wxFormBuilder_win32.zip?branch=master

不要在官网直接下载,感觉那个根本没跟新,跟新的已经放到了github上

主页面:https://github.com/wxFormBuilder/wxFormBuilder

官网:https://sourceforge.net/projects/wxformbuilder/

Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a ...

Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a ...

当我们使用gson 转对象时,并且这个对象中有一些属性是懒加载时如

@Entity
@Table(name = "user")
public class User {
    @Id
    @Column(columnDefinition="varchar(255)")
    @GeneratedValue(generator="idGenerator")
    @GenericGenerator(name="idGenerator", strategy="uuid")
    private String userId;
    private String userName;
    private String nickName;
    private String passWord;
    private String phoneNumber;

    @ManyToOne(cascade = {CascadeType.ALL},optional = true,fetch = FetchType.LAZY)
    @JoinColumn(name = "enterprise_Id")
    private EnterpriseInfo enterpriseInfo;
    }

会出现Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?

原因是:

hibernate采取懒加载的方式查询数据库,也就是只有用到了才去查真正的数据,用不到的话只是返回一个代理对象,gson识别不了代理对象,所以没法转换.

解决办法:

1.新建一个类,并继承TypeAdapter

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import org.hibernate.Hibernate;
import org.hibernate.proxy.HibernateProxy;

import java.io.IOException;

/**
 * @ClassName HibernateProxyTypeAdapter
 * @Description 解决gson转换中对象有懒加载情况报错:
 * @Author PC
 * @Date 2018/10/29 10:23
 **/
public class HibernateProxyTypeAdapter extends TypeAdapter<HibernateProxy> {

    public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
        @Override
        public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
            return (HibernateProxy.class.isAssignableFrom(typeToken.getRawType())? (TypeAdapter) new HibernateProxyTypeAdapter(gson):null);
        }
    };

    private final Gson context;
    private HibernateProxyTypeAdapter(Gson gson){
        this.context = gson;
    }
    @Override
    public void write(JsonWriter jsonWriter, HibernateProxy hibernateProxy) throws IOException {
        if (hibernateProxy==null){
            jsonWriter.nullValue();
            return;
        }
        Class<?> baseType = Hibernate.getClass(hibernateProxy);
        TypeAdapter delegate = context.getAdapter(TypeToken.get(baseType));
        Object unproxiedValue = ((HibernateProxy) hibernateProxy).getHibernateLazyInitializer()
                .getImplementation();
        delegate.write(jsonWriter,unproxiedValue);
    }

    @Override
    public HibernateProxy read(JsonReader jsonReader) throws IOException {
        throw new UnsupportedOperationException("Not support");
    }
}

2.构建gson对象

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd")
      .registerTypeAdapterFactory( HibernateProxyTypeAdapter.FACTORY).create() ; 

3.完成.

参考别人的博客并加以整理.

Grails 2.4 + Hibernate 4 +可搜索的插件= ClassNotFoundException:org.hibernate.impl.SessionFactoryImpl

Grails 2.4 + Hibernate 4 +可搜索的插件= ClassNotFoundException:org.hibernate.impl.SessionFactoryImpl

最近,我将一个简单的Grails应用程序与Searchable插件集成在一起。我发现,Searchable插件不适用于Hibernate 4库。

在这里您可以找到一个示例应用程序,其中仅包含干净的Grails 2.4应用程序,仅添加了Searchable插件-https:
//github.com/wololock/grails-searchable-
example

当我运行该应用程序时:

runtime ":hibernate4:4.3.5.5"

依赖关系,它不会启动并引发异常:

ClassNotFoundException: org.hibernate.impl.SessionFactoryImpl

我已经发现,在Hibernate4中,SessionFactoryImpl已移至org.hibernate.internal包中,并且Compass似乎在旧位置中查找此类:

2014-10-11 19:41:58,142 [localhost-startStop-1] ERROR context.GrailsContextLoaderListener  - Error initializing the application: org/hibernate/impl/SessionFactoryImpl
Message: org/hibernate/impl/SessionFactoryImpl
Line | Method
->>   95 | injectLifecycle in org.compass.gps.device.hibernate.lifecycle.DefaultHibernateEntityLifecycleInjector

切换回:

runtime ":hibernate:3.6.10.17"

和改变

cache.region.factory_

在DataSource.groovy中解决了该问题。

我的问题是: 是否可以在Hibernate 4中使用Searchable插件,或者我们必须等待或解决Compass /
Searchable源代码中的问题?您如何在Grails应用程序中处理该问题?谢谢您的提示。

HibernateProxy. Forgot to register a type adapter?

HibernateProxy. Forgot to register a type adapter?

使用Gson转换Hibernate对象遇到一个问题,当对象的Lazy加载的,就会出现上面的错误。处理方式摘抄自网上,留存一份以后自己看。

/**
 * This TypeAdapter unproxies Hibernate proxied objects, and serializes them
 * through the registered (or default) TypeAdapter of the base class.
 */
public class HibernateProxyTypeAdapter extends TypeAdapter<HibernateProxy> {

    public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
        @Override
        @SuppressWarnings("unchecked")
        public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
            return (HibernateProxy.class.isAssignableFrom(type.getRawType()) ? (TypeAdapter<T>) new HibernateProxyTypeAdapter(gson) : null);
        }
    };
    private final Gson context;

    private HibernateProxyTypeAdapter(Gson context) {
        this.context = context;
    }

    @Override
    public HibernateProxy read(JsonReader in) throws IOException {
        throw new UnsupportedOperationException("Not supported");
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public void write(JsonWriter out, HibernateProxy value) throws IOException {
        if (value == null) {
            out.nullValue();
            return;
        }
        // Retrieve the original (not proxy) class
        Class<?> baseType = Hibernate.getClass(value);
        // Get the TypeAdapter of the original class, to delegate the serialization
        TypeAdapter delegate = context.getAdapter(TypeToken.get(baseType));
        // Get a filled instance of the original class
        Object unproxiedValue = ((HibernateProxy) value).getHibernateLazyInitializer()
                .getImplementation();
        // Serialize the value
        delegate.write(out, unproxiedValue);
    }
}

实现上面的类,然后就是使用:

GsonBuilder b = new GsonBuilder();
...
b.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);
...
Gson gson = b.create();

解释,只是看不懂。

GSON contains a number of TypeAdapterFactory implementations, for various types (primitive types, common types like String or Date, lists, arrays...). Each factory is asked if it is able to serialize a certain Java type (the parameter to create is a TypeToken instead of a Class in order to capture possible information about generic types, which Class does not have). If the factory is able to serialize/deserialize a type, it responds with a TypeAdapter instance; otherwise it responds with null.

HibernateProxyTypeAdapter.FACTORY verifies whether type implements HibernateProxy; in that case, it returns an instance of HibernateProxyTypeAdapter for serialization. The write method is called when an actual object has to be serialized; the adapter extracts the original type of the underlying object, and asks GSON for the standard TypeAdapter for the original type, which generally is a ReflectiveTypeAdapter.

Then it retrieves an instance of the original class, instead of directly using the proxy. This is necessary because ReflectiveTypeAdapter accesses directly to fields, instead of using getters; accessing to the fields of a proxied object does not work, and is a classical Hibernate pitfall.

As a possible performance improvement, the delegate TypeAdapter should be acquired in the create method. I found out that calling getSuperclass() on the proxy Class appears to yield the original base class. 


今天关于Type definition error: [simple type, class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor];的分享就到这里,希望大家有所收获,若想了解更多关于34-TypeError: BoxSizer.AddSpacer(): argument 1 has unexpected type ''tuple''、Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a ...、Grails 2.4 + Hibernate 4 +可搜索的插件= ClassNotFoundException:org.hibernate.impl.SessionFactoryImpl、HibernateProxy. Forgot to register a type adapter?等相关知识,可以在本站进行查询。

本文标签: