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?