Thanks Patrick. You are right. It works now.
Albert
----- Original Message -----
From: "Patrick van Kann"
<[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, October 30, 2002 5:14 PM
Subject: Re: [castor-dev] one-to-many relationship, foreign
key not written to DB
> The relationship between worker and task needs to be
bi-directional at
> the object level and in the mapping. This is demonstrated
in the JDO
> examples.
>
> Therefore, in Worker you require the following change
>
> public void addTask(Task aTask)
> {
> this.tasks.add(aTask);
> aTask.setWorker(this);
> }
>
> Additionally, your mapping for Task needs to specify the
relationship
> back to the Worker.
>
> <class
name="sample.imtf.ch.atlas.pollux.jdo.one2many.Task"
> identity="id">
>
> <description>Task detail</description>
> <map-to table="task"/>
>
> <field name="id" type="integer">
> <sql name="id" type="integer"/>
> </field>
> <field name="description" type="string">
> <sql name="description" />
> </field>
>
> <field name="worker"
> type="sample.imtf.ch.atlas.pollux.jdo.one2many.Task">
> <sql name="worker_id">
> </field>
>
> </class>
>
> Hope this helps. I strongly recommend examining the
examples closely for
> further pointers,
>
> Patrick
>
> Albert Yu wrote:
>
> > <<one2many-Sample.zip>>
> >
> > Hi,
> >
> > I am using Castor 0.9.4 and testing a one-to-many case.
I found the
> > result is wrong. The value of the foreign key (for the
"many") is not
> > written to the database. I have attached the source
code including DB
> > script, mapping.xml to this mail.
> >
> > I paste some parts here for a quick look:
> >
> > **************************************************
> > The result:
> >
> > a) Table "worker"
> > id, firstname, lastname, date_of_birth
> > ------------------------------
> > 1, Jason, Mchuge, 01/01/70
> >
> > b) Table "tasks"
> >
> > id, description, worker_id
> > ------------------------
> > 1, Task1, null
> > 2, Task2, null
> > 3, Task3, null
> >
> > **************************************************
> >
> > As you see nothing is written to the "worker_id" field.
> >
> >
> >
> >
> > -- ====================================
> > -- One to many
> > -- ====================================
> >
> > create table worker
> > (
> > id integer not null,
> > firstname varchar(40) not null,
> > lastname varchar(40) not null,
> > date_of_birth date,
> > primary key (id)
> > );
> >
> > create table task
> > (
> > id integer not null,
> > description varchar(255),
> > worker_id integer references worker(id),
> > primary key (id)
> > );
> >
> > <-- the mapping -->
> >
> > <!-- each worker has many tasks-->
> > <field name="tasks"
> > type="sample.imtf.ch.atlas.pollux.jdo.one2many.Task"
> > collection="arraylist"
> > required="false"
> > set-method="setTasks" get-method="getTasks">
> > <sql many-key="worker_id"/>
> > </field>
> >
> > /**
> > The main program
> > */
> > aDb.begin();
> >
> > Task task1 = new Task(1, "Task1");
> > Task task2 = new Task(2, "Task2");
> > Task task3 = new Task(3, "Task3");
> > aDb.create(task1);
> > aDb.create(task2);
> > aDb.create(task3);
> >
> > Worker worker = new Worker();
> > worker.setId(1);
> > worker.setFirstname("Jason");
> > worker.setLastname("McHuge");
> > worker.setDate_of_birth(new Date(0));
> >
> > worker.addTask(task1);
> > worker.addTask(task2);
> > worker.addTask(task3);
> >
> > aDb.create(worker);
> >
> > aDb.commit();
> >
> >
> > Could anyone help me?
> >
> > Thanks in advance.
> >
> > Albert
>
> ----------------------------------------------------------
-
> If you wish to unsubscribe from this mailing, send mail to
> [EMAIL PROTECTED] with a subject of:
> unsubscribe castor-dev
>
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev