You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-user@db.apache.org by Gabriel Bauman <ga...@bravenet.com> on 2003/02/27 20:46:19 UTC

Mapping fields to subclasses in XML schema

Hi there. I understand that classes and their subclasses are mapped onto 
a single table under Torque. What I don't understand is how to specify 
that a field should only be retrieved in a subclass.

=====
public class SuperClass
{
    public int FOO;
}

public class SubClass extends SuperClass
{
    public int BAR;
}
=====

<table name="superclass" javaName="SuperClass">
   <column name="FOO" type="int" />
   <column name="BAR" type="int" />
   <column name="inheritance" inheritance="single" type="CHAR">
     <inheritance
        class="SubClass"
        extends="SuperClass"
        key="A"
        />
   </column>
</table>

=====
I want to be sure that field BAR does not exist in instances of 
SuperClass, given the fact that they're both mapped on the same table. 
How should my XML schema look in this situation? I must be missing 
something simple here.

Thanks in advance...

Gabe


Re: Mapping fields to subclasses in XML schema

Posted by Gabriel Bauman <ga...@bravenet.com>.
Hmm...

> Since you want BAR in SubClass but not in SuperClass, define your schema 
> as below.
> 
> <table name="superclass" javaName="SuperClass">
>   <column name="FOO" type="INTEGER" inheritance="single">
>     <inheritance key="SubClass" class="SubClass" extends="SuperClass" />
>   </column>
> </table>
> 
> <table name="subclass" javaName="SubClass">
>   <column name="BAR" type="INTEGER" />
> </table>

See, this was what I would have thought, but since Torque's 
documentation says it stores superclasses and subclasses in the same 
table, I don't think what you're saying will actually work. The Torque 
Inheritance Guide (http://db.apache.org/torque/inheritance-guide.html) 
does not mention 'subclass' tables.

Can anyone else who uses inheritance in Torque please settle this? Is 
the above mthod actually correct?

Gabe


Re: Mapping fields to subclasses in XML schema

Posted by Eric Emminger <er...@ericemminger.com>.
I'll take a stab at this! :)

 >>> I want to be sure that field BAR does not exist in instances of
 >>> SuperClass, given the fact that they're both mapped on the same
 >>> table. How should my XML schema look in this situation? I must be
 >>> missing something simple here.

Since you want BAR in SubClass but not in SuperClass, define your schema 
as below.

<table name="superclass" javaName="SuperClass">
   <column name="FOO" type="INTEGER" inheritance="single">
     <inheritance key="SubClass" class="SubClass" extends="SuperClass" />
   </column>
</table>

<table name="subclass" javaName="SubClass">
   <column name="BAR" type="INTEGER" />
</table>

So you know, all I did was generate the OM and look at the Java files. 
Everything looks correct, but that's all I did, so I may have missed 
something.

Please, let me know whether or not this works for you.

Eric


Re: Mapping fields to subclasses in XML schema

Posted by Travis McCauley <tw...@virginia.edu>.
I asked a similar question last Wednesday and also got no replies. 
I've also seen similar questions unanswered in the archives. I don't 
think it's possible to map this in the schema. If it was, either 
someone would say or it would be in the tutorial.

I set up a simple interface to maintain the subclass-to-column 
relationships. If a subclass tries to access an inappropriate field I 
just throw an exception. I also had to override the copy() method to 
allow casting back to the subclass.

Please someone let us know if there is an easier way.

Sincerely,
Travis McCauley
Snowy Virginia




>I don't suppose anyone can enlighten me on the below message? I 
>didn't hear from anyone. Sorry for the spam, I won't ask again. ;)
>
>Gabe
>
>Gabriel Bauman wrote:
>>Hi there. I understand that classes and their subclasses are mapped 
>>onto a single table under Torque. What I don't understand is how to 
>>specify that a field should only be retrieved in a subclass.
>>
>>=====
>>public class SuperClass
>>{
>>    public int FOO;
>>}
>>
>>public class SubClass extends SuperClass
>>{
>>    public int BAR;
>>}
>>=====
>>
>><table name="superclass" javaName="SuperClass">
>>   <column name="FOO" type="int" />
>>   <column name="BAR" type="int" />
>>   <column name="inheritance" inheritance="single" type="CHAR">
>>     <inheritance
>>        class="SubClass"
>>        extends="SuperClass"
>>        key="A"
>>        />
>>   </column>
>></table>
>>
>>=====
>>I want to be sure that field BAR does not exist in instances of 
>>SuperClass, given the fact that they're both mapped on the same 
>>table. How should my XML schema look in this situation? I must be 
>>missing something simple here.
>>
>>Thanks in advance...
>>
>>Gabe
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
>>For additional commands, e-mail: torque-user-help@db.apache.org
>>
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
>For additional commands, e-mail: torque-user-help@db.apache.org


Re: Mapping fields to subclasses in XML schema

Posted by Gabriel Bauman <ga...@bravenet.com>.
I don't suppose anyone can enlighten me on the below message? I didn't 
hear from anyone. Sorry for the spam, I won't ask again. ;)

Gabe

Gabriel Bauman wrote:
> Hi there. I understand that classes and their subclasses are mapped onto 
> a single table under Torque. What I don't understand is how to specify 
> that a field should only be retrieved in a subclass.
> 
> =====
> public class SuperClass
> {
>    public int FOO;
> }
> 
> public class SubClass extends SuperClass
> {
>    public int BAR;
> }
> =====
> 
> <table name="superclass" javaName="SuperClass">
>   <column name="FOO" type="int" />
>   <column name="BAR" type="int" />
>   <column name="inheritance" inheritance="single" type="CHAR">
>     <inheritance
>        class="SubClass"
>        extends="SuperClass"
>        key="A"
>        />
>   </column>
> </table>
> 
> =====
> I want to be sure that field BAR does not exist in instances of 
> SuperClass, given the fact that they're both mapped on the same table. 
> How should my XML schema look in this situation? I must be missing 
> something simple here.
> 
> Thanks in advance...
> 
> Gabe
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
> For additional commands, e-mail: torque-user-help@db.apache.org
>