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 ? 

I have a simple testcase demonstrating the issue. 
I can upload it if needed. 

Regards, 
Ravi.

Reply via email to