You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by janek <op...@gmail.com> on 2007/08/10 17:10:56 UTC

Subclasses instances and query

Hi,
I have problem with query and returned result.

I have three classes: 
  superclass Car, 
  Audi and Hyundai  which extends Car

After persisting some instances of Audi and H., 
query "SELECT c FROM Car c" returns the some 
number of Car instances.

The problem is that result is only of type Car,
I can not cast it up to Audi or Hyunday. 

I understand that this is more efficient, 
but I need full instances, not just superclass. 
Is there any switch which will turn "full subclass fetch" on?

Thanks in advance,
Jan Kotek


 
-- 
View this message in context: http://www.nabble.com/Subclasses-instances-and-query-tf4249297.html#a12093140
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: Subclasses instances and query

Posted by janek <op...@gmail.com>.
I tryed latest 1.0 and problem is still there. I also tryed HSQLDB and it
have the same problem. 

And problem is not definitely on my machine, I reproduced this bug on
another computer by modifing HelloJpa sample which is comming with OpenJPA. 

As I wrote, logs and source codes are here:

http://www.nabble.com/file/p12095237/jpaProblem.zip
http://www.nabble.com/file/p12095237/Fill.log
http://www.nabble.com/file/p12095237/Test.log

Regards,
Jan


janek wrote:
> 
> Hi Craig,
> 
> In attachment is entire Eclipse project. Just add dependencies and
> enhancer agent. It is running on Derby.
> 
> The catch is that you must completely shut down EntityManager and Derby to
> reproduce this problem.
> 
> First run Fill main method, it will create database and put two Audi
> instances there. After that it makes two queries which show that problem
> does not appear here. 
> ie: "SELECT c FROM Car c" returns two Audi instances how it should be
> 
> After thet run Test main method. It will make two same queries, but it
> does not return Audi, but Car. 
> 
> Full log from my machine is attached. 
> 
> Jan
> 
>  http://www.nabble.com/file/p12095237/jpaProblem.zip jpaProblem.zip 
>  http://www.nabble.com/file/p12095237/Fill.log Fill.log 
>  http://www.nabble.com/file/p12095237/Test.log Test.log 
> 

-- 
View this message in context: http://www.nabble.com/Subclasses-instances-and-query-tf4249297.html#a12141773
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: Subclasses instances and query

Posted by janek <op...@gmail.com>.
Hi Craig,

In attachment is entire Eclipse project. Just add dependencies and enhancer
agent. It is running on Derby.

The catch is that you must completely shut down EntityManager and Derby to
reproduce this problem.

First run Fill main method, it will create database and put two Audi
instances there. After that it makes two queries which show that problem
does not appear here. 
ie: "SELECT c FROM Car c" returns two Audi instances how it should be

After thet run Test main method. It will make two same queries, but it does
not return Audi, but Car. 

Full log from my machine is attached. 

Jan

http://www.nabble.com/file/p12095237/jpaProblem.zip jpaProblem.zip 
http://www.nabble.com/file/p12095237/Fill.log Fill.log 
http://www.nabble.com/file/p12095237/Test.log Test.log 
-- 
View this message in context: http://www.nabble.com/Subclasses-instances-and-query-tf4249297.html#a12095237
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: Subclasses instances and query

Posted by Craig L Russell <Cr...@Sun.COM>.
Hi Jan,

Doesn't sound right.

Please post the full code example so we can narrow down what doesn't  
work, in your configuration. Please include your Java sources (test  
case plus entities), mapping information, and database schema; and  
the version of OpenJPA that you're using.

THanks,

Craig

On Aug 10, 2007, at 8:10 AM, janek wrote:

>
> Hi,
> I have problem with query and returned result.
>
> I have three classes:
>   superclass Car,
>   Audi and Hyundai  which extends Car
>
> After persisting some instances of Audi and H.,
> query "SELECT c FROM Car c" returns the some
> number of Car instances.
>
> The problem is that result is only of type Car,
> I can not cast it up to Audi or Hyunday.
>
> I understand that this is more efficient,
> but I need full instances, not just superclass.
> Is there any switch which will turn "full subclass fetch" on?
>
> Thanks in advance,
> Jan Kotek
>
>
>
> -- 
> View this message in context: http://www.nabble.com/Subclasses- 
> instances-and-query-tf4249297.html#a12093140
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Re: Subclasses instances and query

Posted by "David Ezzio (asmtp)" <de...@bea.com>.
Hi Jan,

When issuing a "select c from Car c", one would normally get back 
objects of type Car, but if there are instances of Audi and Hyundai, 
they should not be returned as instances of Car but as instances of Audi 
or Hyundai, which of course, are objects of type Car.  You should be 
able to cast them down.

Since you can't, I suspect that something in your configuration is 
incorrect.  Can you put together an extraordinarily simple test case 
that demos the problem?

Thanks,

David Ezzio



janek wrote:
> Hi,
> I have problem with query and returned result.
> 
> I have three classes: 
>   superclass Car, 
>   Audi and Hyundai  which extends Car
> 
> After persisting some instances of Audi and H., 
> query "SELECT c FROM Car c" returns the some 
> number of Car instances.
> 
> The problem is that result is only of type Car,
> I can not cast it up to Audi or Hyunday. 
> 
> I understand that this is more efficient, 
> but I need full instances, not just superclass. 
> Is there any switch which will turn "full subclass fetch" on?
> 
> Thanks in advance,
> Jan Kotek
> 
> 
>  


Re: Subclasses instances and query

Posted by Kevin Sutter <kw...@gmail.com>.
Janek,
Your problem description sounds very similar to JIRA Issue OPENJPA-143 (
https://issues.apache.org/jira/browse/OPENJPA-143).  At least the first
bullet in the Issue concerning the "requirement" to put the
@DiscriminatorValue on abstract classes.  We are still hoping to get this
resolved in our 1.0.0 release...

Thanks,
Kevin

On 8/14/07, janek <op...@gmail.com> wrote:
>
>
> I know where the problem is.
>
> I am using InheritanceType.SINGLE_TABLE and it seems that OpenJPA does not
> store any class identificatior in table. Information about class is
> reconstructed from NULL and NON NULL fields. So if subclass declares some
> fields which are null, OpenJPA does not have any informations which can
> lead
> to original class and simply use class declareted in Query.
>
> Collumn for this info should be created automaticly as is specified in
> JPA.
>
> http://java.sun.com/javaee/5/docs/api/javax/persistence/DiscriminatorColumn.html
>
> But OpenJPA does not create it automatically. There is entire chapter
> about
> that in manual.
>
> Solution: @DiscriminatorColumn on root class of hierarchy.
>
> Regards,
> Jan
>
> --
> View this message in context:
> http://www.nabble.com/Subclasses-instances-and-query-tf4249297.html#a12143287
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>
>

Re: Subclasses instances and query

Posted by janek <op...@gmail.com>.
I know where the problem is. 

I am using InheritanceType.SINGLE_TABLE and it seems that OpenJPA does not
store any class identificatior in table. Information about class is
reconstructed from NULL and NON NULL fields. So if subclass declares some
fields which are null, OpenJPA does not have any informations which can lead
to original class and simply use class declareted in Query. 

Collumn for this info should be created automaticly as is specified in JPA. 
http://java.sun.com/javaee/5/docs/api/javax/persistence/DiscriminatorColumn.html

But OpenJPA does not create it automatically. There is entire chapter about
that in manual.

Solution: @DiscriminatorColumn on root class of hierarchy.

Regards,
Jan

-- 
View this message in context: http://www.nabble.com/Subclasses-instances-and-query-tf4249297.html#a12143287
Sent from the OpenJPA Users mailing list archive at Nabble.com.