ewade wrote:
I think you may have a fundamental problem with your table design.
What you are describing certainly sounds like a classic many-many relationship between clients and client pools. What you have designed is the client table and the mapping table. I suggest you make a clientpool table with the pool id as the primary key and probably a description field. Then you can use a classic manyTomany mapping that will generate a collection of pools on your client side and a collection of clients on your pool side. You should have a client entity and a pool entity and Hibernate should generate a client table, a pool table and a client-pool mapping table for you.
You are quite correct, although clientpool really has no attributes of it's own even though you suggest a description, but i wouldn't even know what to call it.
In the classic approach, as you've described it ( I think), i would have 3 tables roughly as follows: (but I really need the Pool table to simpy use the ClientID as the primary key, and also map it to a reference to the Client Object)
Code:
Client Pool PoolMapping
CID CID CPID PCID
1 (other stuff) 1 1 2
2 1 3
3 4 2 1
4 2 5
5
I'm only left with figuring out how to map Pool to ClientPool such that the primary key is an entity referencing a Client instance in Client Table.
Okay, so in the above scenario Client1 has a ClientPool that contains Clients 2 and 3.
Client 4, has a ClientPool that contains Clients 1 and 5
I think that is what you are describing to do above? and yes, that is classic.
My only problem would be in convincing my DBA that I needed 3 tables where 2 logically handles it (in his mind). The reality is that i don't need a description only a unique ClientID I can get what i need from 2 tables. If i want to get a list of clients (IDs) in a particular clients pool, the 2 table approach works fine, i simply select ClientIDs from the mapping table.
I'm fine with doing it the classic way (more db access), but i was hoping there was a way that would obviate my need to lobby for it with the DBA, who doesn't know java, or ever heard of hibernate ... and will have a hard time understanding why we need to join 3 tables and not 2. Thus my original question.
I suppose I could write my own persisting class, and instantiate the ClientPool object with my own select from the mapping table, but then i think i'd have to manage the persistance of that collection myself. sigh ... i guess this is the price to pay to allow Hibernate to do that for me.
Perhaps there is no other way.
Thank you both for you input - i'll either move forward in the classic approach, or write more code ....