You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Marcin Skladaniec <ma...@ish.com.au> on 2008/01/17 07:28:26 UTC

merging relationships

Hello

I have encountered an exception when trying to merge relationships  
using this code :

Artist source;
Artist destination;
while (source.getPaintings().size() > 0) {
	Object value = source. getPaintings().get(0);

	source.removeFromPaintings(value);
	destination.addToPaintings(value);
}

This code works for one-to-many relationships, no surprise. For the  
many-to-many relationships with intermediate table with compound pk  
the result is, that upon commit an error message comes up:

The statement was aborted because it would have caused a duplicate key  
value in a unique or primary key constraint or unique index identified  
by 'SQL071114101016080' defined on  
'ARTIST_PAINTING'.java.sql.SQLException: The statement was aborted  
because it would have caused a duplicate key value in a unique or  
primary key constraint or unique index identified by  
'SQL071114101016080' defined on 'ARTIST_PAINTING'

It is a perfectly valid error message, and we have avoided it already,  
but shouldn't cayenne resolve this internally and prevent creating  
double relationship ?

With regards
Marcin

Re: merging relationships

Posted by Marcin Skladaniec <ma...@ish.com.au>.
Hello

Using the naming from the code snippet: The problem is that the  
'value' already exists in the 'destination', and it is added for the  
second time. It is fairly easy to avoid it from happening:

	List<PersistentObject> sourceRelations = new  
ArrayList<PersistentObject>(source.getValueForKey(property));
	List<PersistentObject> initialDestinationRelations = new  
ArrayList<PersistentObject>(destination.getValueForKey(property));
	
	for (PersistentObject po : sourceRelations) {
		if (!initialDestinationRelations.contains(po)) {
			source.removeValueForKey(property, po);
			destination.addValueForKey(property, po);
		}
	}

I think it is a small bug and for simple flattened many-to-many  
relation cayenne should deal with it internally.
Here in this case I expect to create duplicate relationship, but in  
big picture I should always check the current relations before calling  
addToX ?

With regards
Marcin

On 19/01/2008, at 12:43 AM, Andrus Adamchik wrote:

> I don't understand the reason for the error... Did it happen because  
> source and destination are the same object or something? Otherwise I  
> don't see where the duplicate key is created?
>
> Andrus
>
>
> On Jan 17, 2008, at 8:28 AM, Marcin Skladaniec wrote:
>
>> Hello
>>
>> I have encountered an exception when trying to merge relationships  
>> using this code :
>>
>> Artist source;
>> Artist destination;
>> while (source.getPaintings().size() > 0) {
>> 	Object value = source. getPaintings().get(0);
>>
>> 	source.removeFromPaintings(value);
>> 	destination.addToPaintings(value);
>> }
>>
>> This code works for one-to-many relationships, no surprise. For the  
>> many-to-many relationships with intermediate table with compound pk  
>> the result is, that upon commit an error message comes up:
>>
>> The statement was aborted because it would have caused a duplicate  
>> key value in a unique or primary key constraint or unique index  
>> identified by 'SQL071114101016080' defined on  
>> 'ARTIST_PAINTING'.java.sql.SQLException: The statement was aborted  
>> because it would have caused a duplicate key value in a unique or  
>> primary key constraint or unique index identified by  
>> 'SQL071114101016080' defined on 'ARTIST_PAINTING'
>>
>> It is a perfectly valid error message, and we have avoided it  
>> already, but shouldn't cayenne resolve this internally and prevent  
>> creating double relationship ?
>>
>> With regards
>> Marcin
>>
>


Re: merging relationships

Posted by Kevin Menard <km...@servprise.com>.
Obviously I can't speak for Marcin, but this sounds very much like a problem
I had been running into before.

If I added a new object to some entity as a relationship, then removed it
from that entity, but added it to a completely different one, Cayenne would
attempt to do two INSERTs for the same object.  I think I had got around it
by doing a commit after the deletion but before the next addition.  My
memory may be hazy on it though.

-- 
Kevin


On 1/18/08 9:43 AM, "Andrus Adamchik" <an...@objectstyle.org> wrote:

> I don't understand the reason for the error... Did it happen because
> source and destination are the same object or something? Otherwise I
> don't see where the duplicate key is created?
> 
> Andrus
> 
> 
> On Jan 17, 2008, at 8:28 AM, Marcin Skladaniec wrote:
> 
>> Hello
>> 
>> I have encountered an exception when trying to merge relationships
>> using this code :
>> 
>> Artist source;
>> Artist destination;
>> while (source.getPaintings().size() > 0) {
>> Object value = source. getPaintings().get(0);
>> 
>> source.removeFromPaintings(value);
>> destination.addToPaintings(value);
>> }
>> 
>> This code works for one-to-many relationships, no surprise. For the
>> many-to-many relationships with intermediate table with compound pk
>> the result is, that upon commit an error message comes up:
>> 
>> The statement was aborted because it would have caused a duplicate
>> key value in a unique or primary key constraint or unique index
>> identified by 'SQL071114101016080' defined on
>> 'ARTIST_PAINTING'.java.sql.SQLException: The statement was aborted
>> because it would have caused a duplicate key value in a unique or
>> primary key constraint or unique index identified by
>> 'SQL071114101016080' defined on 'ARTIST_PAINTING'
>> 
>> It is a perfectly valid error message, and we have avoided it
>> already, but shouldn't cayenne resolve this internally and prevent
>> creating double relationship ?
>> 
>> With regards
>> Marcin
>> 
> 


Re: merging relationships

Posted by Andrus Adamchik <an...@objectstyle.org>.
I don't understand the reason for the error... Did it happen because  
source and destination are the same object or something? Otherwise I  
don't see where the duplicate key is created?

Andrus


On Jan 17, 2008, at 8:28 AM, Marcin Skladaniec wrote:

> Hello
>
> I have encountered an exception when trying to merge relationships  
> using this code :
>
> Artist source;
> Artist destination;
> while (source.getPaintings().size() > 0) {
> 	Object value = source. getPaintings().get(0);
>
> 	source.removeFromPaintings(value);
> 	destination.addToPaintings(value);
> }
>
> This code works for one-to-many relationships, no surprise. For the  
> many-to-many relationships with intermediate table with compound pk  
> the result is, that upon commit an error message comes up:
>
> The statement was aborted because it would have caused a duplicate  
> key value in a unique or primary key constraint or unique index  
> identified by 'SQL071114101016080' defined on  
> 'ARTIST_PAINTING'.java.sql.SQLException: The statement was aborted  
> because it would have caused a duplicate key value in a unique or  
> primary key constraint or unique index identified by  
> 'SQL071114101016080' defined on 'ARTIST_PAINTING'
>
> It is a perfectly valid error message, and we have avoided it  
> already, but shouldn't cayenne resolve this internally and prevent  
> creating double relationship ?
>
> With regards
> Marcin
>