Eslam Hamdy
Eslam Hamdy

Reputation: 7396

java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy

I'm using a Restful web service (Jersy implementation) with a JSF application and used Json to get the data as follows:

    carObjectDao = new GenericDAO<carObject>(carObject.class);
    List<carObject> allCars = carObjectDao.readAll();
    Gson gson = new Gson();
    String carString = gson.toJson(allCars);
    System.err.println(carString );
    return carString ;

i run the application in debug mode and allCars is filled with the data correctly, but after that an exception is thrown :

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

i don't know the root cause of the exception

Upvotes: 6

Views: 18170

Answers (2)

M.Haris Qayyum
M.Haris Qayyum

Reputation: 137

Try parsing through ObjectMapper as

    carObjectDao = new GenericDAO<carObject>(carObject.class);
    List<carObject> allCars = carObjectDao.readAll();
    String carString = new ObjectMapper().writeValueAsString(allCars);
    System.err.println(carString );
    return carString ;

Upvotes: 0

Ziul
Ziul

Reputation: 893

This is a known problem: Could not serialize object cause of HibernateProxy

JSon can't deserialize HibernateProxy objects, so you either unproxy or remove em.

Or, you can eager fetch the lazy data.

Upvotes: 6

Related Questions