Quantcast

Define a Foreign Key with a One-to-One mapping

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Define a Foreign Key with a One-to-One mapping

Neil Sherman/Trapsoft Inc
Hi,

This may be a bit of a simple question but...

How do I define a foreign key in my Torque schema with a one-to-one
mapping ?  No matter what I do I always seem to end up with a one-to-many
relationship ?

I've tried adding in multiplicity option but it doesnt seem to have any
effect:

                <foreign-key foreignTable="styledJourneys">
                        <option key="bidirectional" value="false"/>
                        <option key="multiplicity" value="one-to-one"/>
                        <reference local="journeySetID" foreign=
"journeySetID"/>
                </foreign-key>

I'm using mySQL 5.1 as my database backend

Thanks for your time

Neil



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Define a Foreign Key with a One-to-One mapping

Thomas Fox-2
> How do I define a foreign key in my Torque schema with a one-to-one
> mapping ?  No matter what I do I always seem to end up with a one-to-many

> relationship ?

There is no support on the java side for a one-to-one relationship (meaning
that a one-to-one relationship is a special case as a
one-to-many-relationship and the generated java code always reflects the
more general one-to-many case)

But you can write the code for a one-to-one relationship yourself:
On the database side, a one-to-one relationship is a non-nullable foreign
key column with a unique index on the foreign key column. All of this can
be defined in the torque schema, e.g. as
...
<column name="journeySetID" required="true"... />
<foreign-key foreignTable="styledJourneys">
  <reference local="journeySetID" foreign="journeySetID"/>
</foreign-key>
<unique>
  <unique-column name="journeySetID"/>
</unique>
...

Then, as you know thatyou have enforced a one-to-one relation on the
database side, you can refelct this knowledge on the java side by adding
getters and setters to your java code which map the n side list to a 1 side
simple getter and setter.

     Hope that helps,

          Thomas


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

tv
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Define a Foreign Key with a One-to-One mapping

tv
On 31.05.11 20:15, Thomas Fox wrote:
> Then, as you know thatyou have enforced a one-to-one relation on the
> database side, you can refelct this knowledge on the java side by adding
> getters and setters to your java code which map the n side list to a 1 side
> simple getter and setter.

You can order the generator to declare your generated getters and
setters as protected and then expose only those you want to publicly
use. I'm not sure if that works with the collection type getters, however.

Bye, Thomas.

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Loading...