[ 
https://issues.apache.org/jira/browse/OPENJPA-1766?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16821591#comment-16821591
 ] 

Ivan Jensen commented on OPENJPA-1766:
--------------------------------------

I have recently stumbled on this issue too, where a polymorphic query doesn't 
return the right subclass (ProductBundleImpl).  Rather all instances are the 
parent class implementation java type (ProductImpl).

This was experienced on our 'slightly customized' openjpa version based on 
openjpa 2.4.0.

It led to a bad case of (higher-level) cache pollution issue and intermittent 
failures which was very tricky to track down.

A hunch made us add 'TYPE(p)' to our 'SELECT' and was really surprised to see 
the query finally work as expected - we assume it triggered different subclass 
resolution logic allowing the appropriate heterogenous lists to be returned.  
That allowed me to find this issue.

If I track down more information/condense the repro conditions I'll report back 
here.

Notes:
* We too have SINGLE_TABLE but my Discriminator is on the parent class of a 
two-class hierarchy (no siblings). 
{noformat}
ProductImpl<--extends---ProductBundleImpl{noformat}

* We too are joining to another entity through an id 
{code:java}
SELECT p, pc.featuredProductOrder, TYPE(p)
FROM ProductImpl p
INNER JOIN p.productCategories pc
WHERE pc.category.uidPk = ?1
AND pc.featuredProductOrder IS NOT null
AND pc.featuredProductOrder <> 0
ORDER BY pc.featuredProductOrder, p.uidPk ASC{code}
* It seems the TYPE(p) is needed when using 'ehcache' as the DataCacheManager; 
it isn't needed when using the default openjpa DataCacheManager.

> Polymorphic queries, selecting all childres - different result depending on 
> select phrase
> -----------------------------------------------------------------------------------------
>
>                 Key: OPENJPA-1766
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-1766
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: jpa
>    Affects Versions: 2.0.0
>         Environment: windows 7, jdk1.5.0_22
>            Reporter: Łukasz Ostaniewicz
>            Priority: Minor
>
> I've got simple inheritance entity tree:
> @Entity 
> @Table(name = "mgruchmagazynowy", schema="magazyn")
> @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
> @DiscriminatorColumn(name="rodzaj",discriminatorType=DiscriminatorType.INTEGER,length=1)
> public class RuchMagazynowy implements Serializable {
>       (...)
> }
> @Entity
> @DiscriminatorValue(value="0")
> public class Dostawa extends RuchMagazynowy {
>       (...)
> }
> @Entity
> @DiscriminatorValue(value="1")
> public class Wydanie extends RuchMagazynowy {
> }
> I've got two rows in "magazyn.mgruchmagazynowy" table: one with "rodzaj" set 
> to 0 and one with "rodzaj" set to 1
> I'm trying to select all children (both Dostawa and Wydanie) and distinguish 
> beetwen them: iterating over the results and using .getClass() or instanceof. 
> My JUNIT test looks like:
>       List<Object[]> list = dao.findRM();
>       for (int i = 0; i < list.size(); i++) {
>               final Object[] row = (Object[]) list.get(i);
>               final Class clazz1 = row[0].getClass();
>               final Class clazz2 = row[1].getClass();
>               log.info("class1:" + clazz1);
>               log.info("class2:" + clazz2);
>       }
> The problem is: I cannot achieve this if I don't specify TYPE(u) amoung 
> SELECT items on my SELECT phrase. I thing the results of my both test cases 
> should be equal (As I think, only test case 2 results gives correct results).
> TEST CASE 1:
>       String select = "SELECT u,e FROM RuchMagazynowy u, 
> ElementRuchuMagazynowego e " +
>               " WHERE u.id = e.ruchMagazynowy.id  ";
>       query = em.createQuery(select);
>       List<Object[]> items = query.getResultList();
> RESULT:
> 2890 [main] INFO org  - class1:class pl.imedia.magazyn.bo.RuchMagazynowy
> 2890 [main] INFO org  - class2:class 
> pl.imedia.magazyn.bo.ElementRuchuMagazynowego
> 2890 [main] INFO org  - class1:class pl.imedia.magazyn.bo.RuchMagazynowy
> 2890 [main] INFO org  - class2:class 
> pl.imedia.magazyn.bo.ElementRuchuMagazynowego
> TEST CASE 2:
>       String select = "SELECT u,e, TYPE(u) FROM RuchMagazynowy u, 
> ElementRuchuMagazynowego e " +
>               " WHERE u.id = e.ruchMagazynowy.id ";
>       query = em.createQuery(select);
>       List<Object[]> items = query.getResultList();
> RESULT:
> 2864 [main] INFO org  - class1:class pl.imedia.magazyn.bo.Dostawa
> 2864 [main] INFO org  - class2:class 
> pl.imedia.magazyn.bo.ElementRuchuMagazynowego
> 2864 [main] INFO org  - class1:class pl.imedia.magazyn.bo.Wydanie
> 2864 [main] INFO org  - class2:class 
> pl.imedia.magazyn.bo.ElementRuchuMagazynowego



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to