Tuesday, April 15, 2014

Re: question about casting

Hi,

Hibernate uses proxies in collections when the objects were loaded from DB. Do not cast to ArrayList, cast instead to List. See http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch07.html#collections-persistent

Juan


2014-04-15 8:24 GMT-03:00 Leung <leung1_2000@yahoo.com>:
Hi,

Can you please explain the casting rule on this example? Why does it cause exception?

Thanks

The userProfileDO is the object extracted from database through hibernate
ArrayList<Contact> tmpContactList = (ArrayList<Contact>) userProfileDO.getContacts();


//warning message: List is a raw type. References to generic type List<E> should be parameterized
    public List getContacts()
    {
        return this.contacts;
    }

The above ArrayList<Contact> casting caused exception
com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract com.project.frontpage.client.serialize.UserProfile com.rentorama2.frontpage.client.rpc.Access.getUserProfile(long) throws com.project.frontpage.client.serialize.LoginServiceException' threw an unexpected exception: java.lang.ClassCastException: org.hibernate.collection.PersistentList cannot be cast to java.util.ArrayList
......
Caused by: java.lang.ClassCastException: org.hibernate.collection.PersistentList cannot be cast to java.util.ArrayList
    at com.project.frontpage.server.dao.UserProfileDAO.getUserProfile(UserProfileDAO.java:250)
    at com.project.frontpage.server.impl.AccessImpl.getUserProfile(AccessImpl.java:655)

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment