-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Many to many relationship with composite id
PostPosted: Mon Jul 18, 2005 8:04 am 
Beginner
Beginner

Joined: Thu Jun 30, 2005 5:15 am
Posts: 21
Hi,

I'm trying to create a recursive many-to-many relationship. The entity has a composite id with three columns and uses a single foreign field to refer to itself again. I've been trying several combinations for the last day, but as far as I understand the hibernate documentation, perhaps this can't be done with the current database model. Unfortunately, I can't change the database schema: other legacy applications depend on it.

So, any help / comment would be really welcome (even if it's a "that's not possible", it'd save me spending even more hours on this). As a way to help visualize the relation, imagine a node of a tree. It just has three fields as a composite key (node_id, node_order, node_app) and it uses a single field to link to its children (linked to the next node_id field).

Thanks in advance,

- Juan


Hibernate version: 3.0.5

Mapping documents:
Code:
  <class table="node_tree" name="comun.menu.model.Node">

    <composite-id name="id" class="comun.menu.modelo.Node$NodeID">
      <key-property name="nodeID" column="node_id"/>
      <key-property name="nodeOrder" column="node_order"/>
      <key-property name="nodeApp" column="node_app"/>
    </composite-id> 

    <list cascade="all" lazy="false" name="children">
      <key column="node_id"/>
      <index column="node_order"/>
      <many-to-many class="comun.menu.model.Node" column="node_child"/>
    </list>

  </class>

Full stack trace of any exception that occurs:

org.hibernate.MappingException: Foreign key (FK5EAE32D5AA8597E:children [node_id])) must have same number of columns as the referenced primary key (node_tree [node_id,node_order, node_app])[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 11:33 am 
Beginner
Beginner

Joined: Thu Jun 30, 2005 5:15 am
Posts: 21
Well, after spending another whole day on this issue, I think I won't map POJOs to the tables. It seems to me that Hibernate wasn't designed to work with composite keys (and this is something I can't change right now -it's the core of the legacy database model- where there are several many-to-many and one-to-many relationships with composite IDs)

Perhaps the only approach is to use the same plain old SQL I was using before. Anyway, any advise about another approach would be truly welcome.

Regards,

- Juan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 12:11 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
It seems to me that Hibernate wasn't designed to work with composite keys


This is, of course, utter nonsense and there are many composite key examples in the documentation. Don't come here and insult my product.

AFAICT, Hibernate can easily handle your case. It might require the use of <key property-ref>, I'm not quite sure. Because neither me nor anyone else here can help you since you didn't bother to show what your actual tables look like.

Stop blaming Hibernate for your shortcomings.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 3:55 am 
Beginner
Beginner

Joined: Thu Jun 30, 2005 5:15 am
Posts: 21
Gavin,

Sorry to hear you feel that way. I don’t come here to "insult and blame" your product, it's just the opposite. As far as I can tell in this short space of time, it's excellent -and from many points of view-.

I didn’t find examples about composite IDs like the one I posted above. The examples of this kind are very simple and I was unable to solve the situations like the ones I described with them. As documents I’m mainly using the Hibernate 3 reference doc and your book Hibernate in Action.

Of course all this is a lack of knowledge of Hibernate, and so I’m posting this here. All the common relationships / mappings were done without problems (and working beautifully).

Here are two simplified examples that illustrate the problem. I think that just one example would solve them all.

** Many to many using a composite key **

Code:
Table A
(PK) A_ID1
(PK) A_ID2
(PK) A_ID3
< more fields>


Code:
Table B
(PK) B_ID1
(PK) B_ID2
(PK) B_ID3
(PK) B_ID4
< more fields>


Code:
Table RELATION_A_B
(FK) A_ID3
(FK) B_ID2


** Recursive (one-to-many) relationship using a composite key **

Code:
Table NODE
(PK) R_ID1
(PK) R_ID2
(PK) R_ID3
(FK) NEXT_NODE_ID <---- has the R_ID3
< more fields >
[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 11:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Use property-ref.

I hope you realize what an incredibly broken data model this is.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.