0

I have a problem to get a Single Location Object from a List. Calling getLocations() in primefaces dataTable works. And I can display all Locations. But when I want to get a Single Location from the locations List, then I get always a ClassCastException. I use JPA (Hibernate).

Entity Bean:

@Entity
@Table(name = "location")
@NamedQueries({
    @NamedQuery(name = "Location.findAll", query = "SELECT l FROM Location l")})
public class Location implements Serializable {
    private static final long serialVersionUID = 1L;

@Column(name = "name")
private String name;
...

CDI Bean:

private List<Location> locations = null;

@PostConstruct
public void init() {
    locations = getService().findAll();
    Iterator iter = getLocations().iterator();
    Location first = (Location)iter.next(); //ClassCastException

Same problem with this:

for (Location location : getLocations()) //Classcastexception
 {...}

error Message:

java.lang.ClassCastException: package.ejb.entity.Location cannot be cast to package.ejb.entity.Location

Update: The Entire Application is an EAR Project. The Location Entity Bean is included in the EJB-Jar. Also LocationService Session Bean, which is injected into the CDI Bean. ( for the findAll() call)

The CDI Bean is included in the WAR.

@shruti1810 mentioned, that there could be a class loading problem. I read the document The Class Loader Hierarchy from Oracle and all I understand is, that the EJB class loader is parent for the WAR class loader. Due to this I don't get why there is a ClassCastException. Is the Location in the CDI Bean loaded from another Class loader than the locations given by the findAll() Method?

0

2 Answers 2

1

It seems as a class loader issue - ClassCastException on the same class is possible when the same class is loaded by two different class loaders, i.e. you have loaded the class with one classloader, then try to cast it to the same class loaded by another classloader.

Here, you can do one of the two things:

Have a common classloader which loads the classes to be used by your custom classloaders. So in your case, you'd have a new classloader which would load the given class and your custom classloaders would extend this classloader.

Another solution would be to pass around the "serialized" state between the two classloaders. Serialize one instance to a byte array and reconstruct the object in the other classloader by de-serializing the object stream.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your response. Is this because I have an EAR Project and the Entity Beans are in the EJB Jar Archive and the CDI Bean is in the WAR archive? And what would be a workaround for this?
Here, you can do one of the two things: Have a common classloader which loads the classes to be used by your custom classloaders. So in your case, you'd have a new classloader which would load the given class and your custom classloaders would extend this classloader. Another solution would be to pass around the "serialized" state between the two classloaders. Serialize one instance to a byte array and reconstruct the object in the other classloader by de-serializing the object stream.
Thanks for your quick response, I'll give it a try and confirm if it works.
I read some documents about class loading, but I still don't know, why the class loading doesn't work. I updated my Start Post.
Fixed it with a downgrade of my hibernate version.
0

Fixed it with a downgrade of my hibernate version from 4.3.9-Final to 4.3.5-Final. I don't no yet what causes this problem. But it seems, that there are several problems with hibernate 4.3.5+.

Another Topic, with a similar fix: here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.