Return-Path: X-Original-To: apmail-cayenne-user-archive@www.apache.org Delivered-To: apmail-cayenne-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id ED4059602 for ; Tue, 12 Jun 2012 13:53:20 +0000 (UTC) Received: (qmail 13305 invoked by uid 500); 12 Jun 2012 13:53:20 -0000 Delivered-To: apmail-cayenne-user-archive@cayenne.apache.org Received: (qmail 13289 invoked by uid 500); 12 Jun 2012 13:53:20 -0000 Mailing-List: contact user-help@cayenne.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@cayenne.apache.org Delivered-To: mailing list user@cayenne.apache.org Received: (qmail 13280 invoked by uid 99); 12 Jun 2012 13:53:20 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 12 Jun 2012 13:53:20 +0000 X-ASF-Spam-Status: No, hits=0.7 required=5.0 tests=FSL_RCVD_USER,SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (nike.apache.org: local policy) Received: from [59.167.240.32] (HELO fish.ish.com.au) (59.167.240.32) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 12 Jun 2012 13:53:14 +0000 Received: from [10.242.2.22] (port=51622 helo=Aris-MacBook-Pro.local) by fish.ish.com.au with esmtpsa (TLSv1:CAMELLIA256-SHA:256) (Exim 4.76) (envelope-from ) id 1SeRWT-0000UH-1X for user@cayenne.apache.org; Tue, 12 Jun 2012 23:52:42 +1000 X-CTCH-RefID: str=0001.0A150203.4FD749AA.003E,ss=1,re=0.000,fgs=0 Message-ID: <4FD749A7.1000208@maniatis.org> Date: Tue, 12 Jun 2012 23:52:39 +1000 From: Aristedes Maniatis User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20120529 Thunderbird/13.0 MIME-Version: 1.0 To: user@cayenne.apache.org Subject: Re: Mapping a relationship with type List References: <3BD3774E5685E142AF9CA634D421DEFF0E2CBF66@spex01.softplant.de> In-Reply-To: <3BD3774E5685E142AF9CA634D421DEFF0E2CBF66@spex01.softplant.de> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit On 12/06/12 7:01pm, Sch�nfisch, J�rg wrote: > Hi, > > I have two tables, one relates to an object entity, the other is a join table. A FK relationship exists between those two. However the "other" IDs in the join table may be present in several different tables so I cannot define a FK relationship for them. Thus I directly want to read a list of IDs and not convert them into object entities. The mapping of IDs to objects will later take place in the business code. > Any hints on how to do this or if it's possible at all? What you are describing is a polymorphic join. In Rails it looks like this: http://guides.rubyonrails.org/association_basics.html#polymorphic-associations There is no built-in construction in Cayenne, but it isn't hard to map. Let's pretend you want to have the ability to add "attachments" to various other entities. Contacts, Artists, Paintings. These attachments might be a picture or other information. So you create: [Attachment] id other columns [AttachmentRelation] foreignRecordId (int) foreignTable (string or enum) [ContactAttachmentRelation] [PaintingAttachmentRelation] [ArtistAttachmentRelation] These last three classes you create in Cayenne model and you make them a subclass of AttachmentRelation. There is no database table that goes with them, so effectively you are creating them as single table inheritance. Three subclasses, and only table table in the database. Put a method like this in the AttachmentRelation: public abstract void setAttachedRelation(Attachable attachable); And then code like this goes in the ContactAttachmentRelation subclass: public void setAttachedRelation(Attachable attachable) { setAttachedContact((Contact) attachable); } The model will have a qualifier like this: There is a bit more to do, but it is pretty simple. The main trick is to use the Cayenne inheritance and let it handle all your SQL generation. It works really nicely and we've had something like this in production for many years. Ari -- --------------------------> Aristedes Maniatis GPG fingerprint CBFB 84B4 738D 4E87 5E5C 5EFA EF6A 7D2E 3E49 102A