openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rpalache <ravi.palache...@oracle.com>
Subject Re: ClasscastException when ClassCrietria is not used.
Date Wed, 22 Jul 2009 21:43:22 GMT

Hi ,

Here is the cause of the issue:

getSelectSubclasses() of ValueMappingImpl.java is checking for criteria.

"return (_criteria) ? Select.SUBS_JOINABLE
                    : Select.SUBS_ANY_JOINABLE;"

If criteria is selected then "Select.SUBS_JOINABLE" is returned which will
generate a select statement that has subclass criteria ( in my E.g:
t0.ITEM_TYPE = ? )

If criteria is not selected then "Select.SUBS_ANY_JOINABLE" is returned
which will not generate the above condition.

So the question is if we have to make Select.SUBS_JOINABLE the default
behavior.

I think throwing class cast exception is bad and hence the default behavior
need to be changed to Select.SUBS_JOINABLE.

Thanks in advance,
Ravi.


rpalache wrote:
> 
> 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.
> 
> 

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

Mime
View raw message