Return-Path: Delivered-To: apmail-db-derby-user-archive@www.apache.org Received: (qmail 90366 invoked from network); 14 Feb 2010 15:44:16 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 14 Feb 2010 15:44:16 -0000 Received: (qmail 17710 invoked by uid 500); 14 Feb 2010 15:44:16 -0000 Delivered-To: apmail-db-derby-user-archive@db.apache.org Received: (qmail 17649 invoked by uid 500); 14 Feb 2010 15:44:16 -0000 Mailing-List: contact derby-user-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: List-Id: Reply-To: "Derby Discussion" Delivered-To: mailing list derby-user@db.apache.org Received: (qmail 17641 invoked by uid 99); 14 Feb 2010 15:44:16 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 14 Feb 2010 15:44:16 +0000 X-ASF-Spam-Status: No, hits=0.7 required=10.0 tests=SPF_SOFTFAIL X-Spam-Check-By: apache.org Received-SPF: softfail (nike.apache.org: transitioning domain of sylvain@chicoree.fr does not designate 80.12.242.1 as permitted sender) Received: from [80.12.242.1] (HELO smtp19.orange.fr) (80.12.242.1) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 14 Feb 2010 15:44:05 +0000 Received: from me-wanadoo.net (localhost [127.0.0.1]) by mwinf1903.orange.fr (SMTP Server) with ESMTP id 6269E2032290; Sun, 14 Feb 2010 16:16:19 +0100 (CET) Received: from me-wanadoo.net (localhost [127.0.0.1]) by mwinf1903.orange.fr (SMTP Server) with ESMTP id E8638206D91A; Sun, 14 Feb 2010 15:25:03 +0100 (CET) Received: from [10.129.36.10] (Mix-Rennes201-1-6.w193-250.abo.wanadoo.fr [193.250.26.6]) by mwinf1903.orange.fr (SMTP Server) with ESMTP id E530020738B5; Sun, 14 Feb 2010 13:34:53 +0100 (CET) X-ME-UUID: 20100214123453938.E530020738B5@mwinf1903.orange.fr X-ME-User-Auth: sonia.jouanno Message-ID: <4B77EDED.8050201@chicoree.fr> Date: Sun, 14 Feb 2010 13:34:53 +0100 From: Sylvain Leroux User-Agent: Thunderbird 2.0.0.23 (Macintosh/20090812) MIME-Version: 1.0 To: Derby Discussion Subject: Re: self referencing column References: <4B669D03.5070202@yahoo.de> <4B67F584.90007@yahoo.de> In-Reply-To: <4B67F584.90007@yahoo.de> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Checked: Checked by ClamAV on apache.org Hi Joe, Maybe this is a "too late" answer, but I think one reason you experiencing some problems here, is that you are using a single table to represent a N-to-N relationship. Why not instead using two tables: - one for the states - one for the transitions The "state" table could have an "id" GENERATED ALWAYS AS IDENTITY, and the states on your "transition" table could have the required REFERENCES clause. Something like that: CREATE TABLE state ( id int GENERATED ALWAYS AS IDENTITY -- possibly other state-related attributes here ); CREATE TABLE transition ( start int NOT NULL REFERENCES state(ID), end int NOT NULL REFERENCES state(ID), -- possibly other transition-related attributes here ) Inserting a new self-referencing state would become: INSERT INTO state() VALUES(); INSERT INTO transition (start, end) VALUES (IDENTITY_VAL_LOCAL(), IDENTITY_VAL_LOCAL()); According to the documentation [1], since IDENTITY_VAL_LOCAL returns the last generated identity for a /connection/, I think this does not require a specific isolation level to deal with possible concurrent access to you DB. [1] http://db.apache.org/derby/docs/10.5/ref/rrefidentityvallocal.html Sylvain joe a �crit : > > Hi, >> Perhaps another way to work around this is to use GENERATED BY DEFAULT as >> IDENTITY. >> >> > > But how? > I dont want the user to define the id. > > Ok i can use a helper table which holds the id > create table state_id > ( > ID int not null > ) > insert into state_id values (0) > > and doing the follwing two commands for every state insert: > update state_id set id = ((select id from state_id) +1) > insert into state values ((select id from state_id), (select id from > state_id)) > > Looks quite ugly for me. > > > -- sylvain@chicoree.fr http://www.chicoree.fr