You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by rpalache <ra...@oracle.com> on 2009/07/17 23:49:44 UTC

ClasscastException when ClassCrietria is not used.

Hi All,

I have an openJPA application with following entities:

"Item" Entity with SINGLE_TABLE inheritance strategy.
 DiscriminatorColumn on this ITEM_TYPE.

"Book" and "Movie" entities extend "Item."

"Artist" entity has 1-many relationship with "Book."

The following results in classcastexception :
Artist artist = em.find(Artist.class, "Herman Hess");
List<Book> books= artist.getBooks();

"artist.getBooks()" results in rows that contain both "Book" and"Movie"
hence I get the following classcast :
Exception in thread "main" java.lang.ClassCastException:
org.apache.openjpa.enhance.model$Movie$pcsubclass

When I add @ElementClassCriteria to the Artist to Book relationship then
that resolves the issue.
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER,
mappedBy="artist")
@org.apache.openjpa.persistence.jdbc.ElementClassCriteria
public List<Book> books;

The reason for this issue is :
without @ElementClassCriteria the SQL generated is :
SELECT t0.UID1, t0.ITEM_TYPE, t0.title, t0.PAGE_COUNT FROM ITEM t0 WHERE
t0.ARTIST = ? 
[params=(String) Herman Hess]

With @ElementClassCriteria the SQL generated is :
SELECT t0.UID1, t0.ITEM_TYPE, t0.title, t0.PAGE_COUNT FROM ITEM t0 WHERE
t0.ARTIST = ? AND t0.ITEM_TYPE = ? 
[params=(String) Herman Hess, (String) BOOK]

My questions regarding this is as follows:

1) @ElementClassCriteria is an openJPA specific annotation. Can I have a JPA
specific solution for this problem ?
2) If I have a very big application(s) and lots of relations with the above
described behavior.
   Then do I have to add it to each and every entity class that has the
relationship ?
   Is there any property that I can define in the persistence.xml rather
than in entities ?

Regards,
Ravi.
-- 
View this message in context: http://n2.nabble.com/ClasscastException-when-ClassCrietria-is-not-used.-tp3277998p3277998.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.