You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@turbine.apache.org by Julián Listo <ju...@hotmail.com> on 2002/02/24 21:04:20 UTC

Torque and 1:n mapping: examples?

Hi all!

I'm looking around at OR frameworks, lurked on the Turbine mailing list a 
while and I have a question that was picked up by Leon Messerschmidt 
<le...@opticode.co.za> who said:

>Torque will generate methods for you to access the relevant objects.
>For example Category and Item.  Category.getItems() will give all the 
> >items for a category or Item.getCategory() will give the associated 
> >Category for an Item.
>Torque also generates methods for joining all objects with less db >hits as 
>to improve performance.

This is great... problem I have is that I can't find any docs that describe 
how to do this. I've been browsing through Scarab 1.0b4 source too but it 
appears a lot of that is hand-coded... or perhaps I'm not clear exactly what 
the syntax is for the Turbine schema.xml? I would've thought it would be 
part of the foreign-key definition but nothing there makes sense to me.

Any suggestions would be greatly appreciated!

cheers all (and particularly Leon for responding to the first question),

Julian.



_________________________________________________________________
Con MSN Hotmail súmese al servicio de correo electrónico más grande del 
mundo. http://www.hotmail.com/ES


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Torque and 1:n mapping: examples?

Posted by Bill Schneider <bs...@vecna.com>.
Hi Julián:

You get Torque to generate Java code to support 1..n relations by specifying
foreign-key constraints in your schema, just as you would if you were
writing the SQL data model directly.  This tells Torque how the objects it
generates for each table are related.

If you have a relation between Categories and Items (1 .. n), you represent
this in the schema by having each Item reference its Category:

<table name="category">
  <column name="category_id" type="INTEGER" primaryKey="true"/>
  <column name="description" type="VARCHAR"/>
</table>

<table name="item">
  <column name="item_id" type="INTEGER" primaryKey="true"/>
  <column name="category_id" type="INTEGER"/>
  <column name="description" type="varchar"/>
  <!-- this is the foreign key magic -->
  <foreign-key foreignTable="category">
    <reference local="category_id" foreign="category_id"/>
  </foreign-key>
</table>

This will cause Torque to generate a method in the Item class "getCategory()
[returns Category]" in addition to the method "getCategoryId() [returns
int]" that would have been generated with or without the foreign-key
constraint.

On the other side, Torque will generate methods in Category: addItem(Item)
and getItems() [returns List of Item].

Other important things to note:

- if you have a compound primary key, you must put two "reference" elements
inside the same "foreign-key" element.  Making two separate foreign-key
elements won't work.

- if you reference the same table with two different foreign keys (e.g.,
instead of category_id in the above example, suppose you have
"old_category_id" and "new_category_id" which both point to Category) you
will get a method in Item for each foreign key linking a Category to
distinguish the two: getCategory: getCategoryRelatedByOldCategoryId and
getCategoryRelatedByNewCategoryId.

Hope this answered your question!

----- Original Message -----
From: "Julián Listo" <ju...@hotmail.com>
To: <tu...@jakarta.apache.org>
Sent: Sunday, February 24, 2002 3:04 PM
Subject: Torque and 1:n mapping: examples?


> Hi all!
>
> I'm looking around at OR frameworks, lurked on the Turbine mailing list a
> while and I have a question that was picked up by Leon Messerschmidt
> <le...@opticode.co.za> who said:
>
> >Torque will generate methods for you to access the relevant objects.
> >For example Category and Item.  Category.getItems() will give all the
> > >items for a category or Item.getCategory() will give the associated
> > >Category for an Item.
> >Torque also generates methods for joining all objects with less db >hits
as
> >to improve performance.
>
> This is great... problem I have is that I can't find any docs that
describe
> how to do this. I've been browsing through Scarab 1.0b4 source too but it
> appears a lot of that is hand-coded... or perhaps I'm not clear exactly
what
> the syntax is for the Turbine schema.xml? I would've thought it would be
> part of the foreign-key definition but nothing there makes sense to me.
>
> Any suggestions would be greatly appreciated!
>
> cheers all (and particularly Leon for responding to the first question),
>
> Julian.
>
>
>
> _________________________________________________________________
> Con MSN Hotmail súmese al servicio de correo electrónico más grande del
> mundo. http://www.hotmail.com/ES
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Torque and 1:n mapping: examples?

Posted by Christian Asmussen <kr...@kriconet.com.br>.
http://jakarta.apache.org/turbine/torque/tutorial.html

look at autor (author_id) will solve your problem.


On Sun, 24 Feb 2002, Julián Listo wrote:

> Hi all!
> 
> I'm looking around at OR frameworks, lurked on the Turbine mailing list a 
> while and I have a question that was picked up by Leon Messerschmidt 
> <le...@opticode.co.za> who said:
> 
> >Torque will generate methods for you to access the relevant objects.
> >For example Category and Item.  Category.getItems() will give all the 
> > >items for a category or Item.getCategory() will give the associated 
> > >Category for an Item.
> >Torque also generates methods for joining all objects with less db >hits as 
> >to improve performance.
> 
> This is great... problem I have is that I can't find any docs that describe 
> how to do this. I've been browsing through Scarab 1.0b4 source too but it 
> appears a lot of that is hand-coded... or perhaps I'm not clear exactly what 
> the syntax is for the Turbine schema.xml? I would've thought it would be 
> part of the foreign-key definition but nothing there makes sense to me.
> 
> Any suggestions would be greatly appreciated!
> 
> cheers all (and particularly Leon for responding to the first question),
> 
> Julian.
> 
> 
> 
> _________________________________________________________________
> Con MSN Hotmail súmese al servicio de correo electrónico más grande del 
> mundo. http://www.hotmail.com/ES
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 
> 

-- 
"If we did all the things we are capable of, 
we would literally astound ourselves"
 - Thomas Edison


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>