Hi Michael

Yes, the qualifier is filled in. This cayenne model was in use for a long time, we only added some NOT NULL and UNIQUE constraints recently, and now the error showed up.


This is what I do (in java) and relevant sql logs (simplified):
DataContext cc = AngelServer.getDatabaseConnector().getNewContext();
            Site s = queryForTheSite();
            SiteTagRelation str = s.getTaggingRelations().get(0);
            Tag t = str.getTag();

SELECT t0.createdOn, t0.id... FROM Site t0
org.apache.cayenne.access.QueryLogger :401 - === returned 1 row. - took 127 ms.

SELECT t0.createdOn, t0.id... FROM TagRelation t0 WHERE (t0.entityRecordId = ?) AND (t0.entityIdentifier = ?) [bind: 1->entityRecordId:280, 2->entityIdentifier:9] org.apache.cayenne.access.QueryLogger :401 - === returned 1 row. - took 164 ms.

SELECT t0.createdOn, t0.id... FROM Tag t0 WHERE t0.id = ? [bind: 1->id:460]
org.apache.cayenne.access.QueryLogger :401 - === returned 1 row. - took 169 ms.


            logger.warn("deleting tag relation: " + str);
            cc.deleteObject(str);
            logger.warn("tag relation deleted...");

deleting tag relation: {<ObjectId:SiteTagRelation, id=542>; committed; [tag=>?; entityIdentifier=>9; taggedSite=>?]}
tag relation deleted...


            SiteTagRelation strNew = cc.newObject(SiteTagRelation.class);
            strNew.setTag(t);
            strNew.setTaggedRelation(s);
            logger.warn("tag relation created: " + strNew);

tag relation created: {<ObjectId:SiteTagRelation, TEMP:0000004ABC950164>; new; [tag=>{<ObjectId:Tag, id=460>}; entityIdentifier=>9; taggedSite=>{<ObjectId:Site, id=280>}]}

            cc.commitChanges();

SELECT NEXT_ID FROM AUTO_PK_SUPPORT WHERE TABLE_NAME = ? FOR UPDATE [bind: 1:'TagRelation'] INSERT INTO TagRelation (entityIdentifier, entityRecordId, id, tagId) VALUES (?, ?, ?, ?)

org.apache.cayenne.access.QueryLogger :372 - [batch bind: 1->entityIdentifier:9, 2->entityRecordId:280, 3->id:560, 5->tagId:460]
org.apache.cayenne.access.QueryLogger :453 - *** error.
java.sql.SQLIntegrityConstraintViolationException: 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 'TAGRELATION_UNIQUE' defined on 'TAGRELATION'.


As you can see the insert is executed before delete.

If the context is committed after deleting its all fine.

As you can guess nullifying the relationships manually before deleting object, makes no difference.

With regards
Marcin

On 15/02/11 11:47 PM, Michael Gentry wrote:
Hi Marcin,

Most likely your unique index is causing some grief.  Any chance you
are logging the SQL and can post it?

Also, is "entityIdentifier" being used for inheritance in Cayenne?
(Did you fill something in the "Qualifier" field in the ObjEntity?)
If so, it sounds like you are using it as a singleton?

Thanks,

mrg


On Mon, Feb 14, 2011 at 10:04 PM, Marcin Skladaniec<mar...@ish.com.au>  wrote:
Hello

I have run into a problem which seems to be a bug in cayenne. I have tried
several approaches in order to isolate the direct cause, thinking it is some
kind of misconfiguration in my code, yet no luck.


Using  cayenne 3.0, derby (not tested on other db so far).
My cayenne model defines an entity called SiteTagRelation, inheriting from
TagRelation.
TagRelation is based on database entity with two important fields:
entityRecordId and entityIdentifier. The latter is used for inheritance,
identifying the type of TagRelation, the former is used as foreign key. Both
fields are mandatory, and there is an unique index on the TagRelation table,
created with following :
CREATE UNIQUE INDEX TagRelation_unique ON TagRelation (nodeId,
entityIdentifier, entityRecordId)

For this example the final relationships are as follow: Site -<
SiteTagRelation>- Tag

Now a simple scenario of:
- getting a context
- fetching a Site
- deleting one SiteTagRelation using objectContext.deleteObject()
- re-creating new SiteTagRelation to the same Tag and Site using
objectContext.newObject()

At this moment the context contains:
  Tag, persistent state = committed
  Site, persistent state = committed
  SiteTagRelation, persistent state = deleted
  SiteTagRelation, persistent state = new

but commitChanges() yields
java.sql.SQLIntegrityConstraintViolationException: 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 'TAGRELATION_UNIQUE' defined on
'TAGRELATION'.


Please can someone confirm that they have the same problem... or not, which
means I have to dig deeper into my code and find what I'm doing wrong.

Best regards!
Marcin
-------------------------->
ish
http://www.ish.com.au
Level 1, 30 Wilson Street Newtown 2042 Australia
phone +61 2 9550 5001   fax +61 2 9550 4001


Reply via email to