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 Matteo Barbieri <m....@ste-energy.com> on 2006/02/17 11:05:16 UTC

Saving two related objects

Hi,
maybe it's a stupid question, but I got an error that I don't know why
it happens.

I have two tables that are related by foreign key.

I have to create one istance of both objects and to save them.

I do something like this:

FirstClass first = new FirstClass();
SecondClass second = new SecondClass();

first.save();

second.setFirstClass(first);
second.save();

the first object is saved correctly, but then I got an SQLException:

java.sql.SQLException: Cannot add or update a child row: a foreign key
constraint fails

I've tried to print both objects before every saving and the second
object has the correct foreign key.

Any idea?

Thanks

-- 
Matteo Barbieri
S.T.E. s.r.l.
m.barbieri@ste-energy.com


---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org


Re: Saving two related objects

Posted by Matteo Barbieri <m....@ste-energy.com>.
Matteo Barbieri ha scritto:

>Thomas Fischer ha scritto:
>
>  
>
>>I had just a 5 seconds glance on this, so maybe I'm completely wrong. Have
>>you tried  to set the attribute javaType="object" in the foreign key's
>>column definition in the schema.xml ?
>>
>>  Thomas
>>
>>Matteo Barbieri <m....@ste-energy.com> schrieb am 17.02.2006 15:29:22:
>> 
>>    
>>

Now I found!!!
The error occurs because the foreign key was also the primary key of the
table.
I tried to remove the primary key constraint and now all works.

I don't know if it's a feature or a bug of Torque.

-- 
Matteo Barbieri



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org


Re: Saving two related objects

Posted by Matteo Barbieri <m....@ste-energy.com>.
Thomas Fischer ha scritto:

>I had just a 5 seconds glance on this, so maybe I'm completely wrong. Have
>you tried  to set the attribute javaType="object" in the foreign key's
>column definition in the schema.xml ?
>
>   Thomas
>
>Matteo Barbieri <m....@ste-energy.com> schrieb am 17.02.2006 15:29:22:
>  
>

No, now I try.

---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org


Re: Saving two related objects

Posted by Matteo Barbieri <m....@ste-energy.com>.
Thomas Fischer ha scritto:

>I had just a 5 seconds glance on this, so maybe I'm completely wrong. Have
>you tried  to set the attribute javaType="object" in the foreign key's
>column definition in the schema.xml ?
>
>   Thomas
>  
>
Just tried, same error.

-- 
Matteo Barbieri



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org


Re: Saving two related objects

Posted by Thomas Fischer <fi...@seitenbau.net>.
I had just a 5 seconds glance on this, so maybe I'm completely wrong. Have
you tried  to set the attribute javaType="object" in the foreign key's
column definition in the schema.xml ?

   Thomas

Matteo Barbieri <m....@ste-energy.com> schrieb am 17.02.2006 15:29:22:

> After some more tests I found that when I save the second object, Torque
> tries to save it with 0 as foreign key, even if second.FkFirstClass()
> gives me the correct id of first.
>
> So what do I do?
>
> Bye
>
> Matteo Barbieri ha scritto:
>
> >Hi,
> >maybe it's a stupid question, but I got an error that I don't know why
> >it happens.
> >
> >I have two tables that are related by foreign key.
> >
> >I have to create one istance of both objects and to save them.
> >
> >I do something like this:
> >
> >FirstClass first = new FirstClass();
> >SecondClass second = new SecondClass();
> >
> >first.save();
> >
> >second.setFirstClass(first);
> >second.save();
> >
> >the first object is saved correctly, but then I got an SQLException:
> >
> >java.sql.SQLException: Cannot add or update a child row: a foreign key
> >constraint fails
> >
> >I've tried to print both objects before every saving and the second
> >object has the correct foreign key.
> >
> >Any idea?
> >
> >Thanks
> >
> >
> >
>
>
> --
> Matteo Barbieri
>
>
>
> ---------------------------------------------------------------------
> 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: Saving two related objects

Posted by Matteo Barbieri <m....@ste-energy.com>.
After some more tests I found that when I save the second object, Torque
tries to save it with 0 as foreign key, even if second.FkFirstClass()
gives me the correct id of first.

So what do I do?

Bye

Matteo Barbieri ha scritto:

>Hi,
>maybe it's a stupid question, but I got an error that I don't know why
>it happens.
>
>I have two tables that are related by foreign key.
>
>I have to create one istance of both objects and to save them.
>
>I do something like this:
>
>FirstClass first = new FirstClass();
>SecondClass second = new SecondClass();
>
>first.save();
>
>second.setFirstClass(first);
>second.save();
>
>the first object is saved correctly, but then I got an SQLException:
>
>java.sql.SQLException: Cannot add or update a child row: a foreign key
>constraint fails
>
>I've tried to print both objects before every saving and the second
>object has the correct foreign key.
>
>Any idea?
>
>Thanks
>
>  
>


-- 
Matteo Barbieri



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org