-->
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.  [ 4 posts ] 
Author Message
 Post subject: Constrained one-to-many relationship
PostPosted: Wed Aug 31, 2005 11:59 pm 
Newbie

Joined: Wed Aug 31, 2005 11:03 pm
Posts: 2
Im just learning Hibernate so this should be a good opportunity to earn some credits :)

My problem is as follows. I am trying to create mapping files for a hierarchical relationship. The basic idea is this. I have (0-N) accounts. Each account has (0-N) computers. Each computer has (0-N) connections. Pretty simple huh? Here's the catch.

I want the accounts table to have a generated primary key. No problem.

I want the computers table to have a generated (primary?) key which is unique to each account (FK?) but not to the table as a whole. An example table might be as follows:

Code:
Computers
+-----------------------------------
| account_id  | computer_id   | ... all_the_other_computer_columns
+-------------+---------------+---
| 1           | 1             |
+-------------+---------------+---
| 1           | 2             |
+-------------+---------------+---
| 1           | 3             |
+-------------+---------------+---
| 2           | 1             |
+-------------+---------------+---

And so on....

Likewise, I would like to have the connections table have a generated (primary?) key which is unique to the (account,machine) pair but not to the table as a whole. For example:

Code:
Connections
+-----------------------------------------------------
| account_id  | computer_id   | connection_id | ...
+-------------+---------------+---------------+-----
| 1           | 1             | 1             |
+-------------+---------------+---------------+-----
| 1           | 1             | 2             |
+-------------+---------------+---------------+-----
| 1           | 3             | 1             |
+-------------+---------------+---------------+-----
| 2           | 1             | 2             |
+-------------+---------------+---------------+-----


My question is, how do I set up the mapping files to handle this situation? I have searched through the forum, the online docs, and "Hibernate, A J2EE Developer's Guide" to no avail.

Clarification would be greatly appreciated. Also, feel free to make suggestions about my database schema if you think there is a better way to do it.


Top
 Profile  
 
 Post subject: Re: Constrained one-to-many relationship
PostPosted: Thu Sep 01, 2005 5:53 am 
Beginner
Beginner

Joined: Tue Jul 19, 2005 4:03 am
Posts: 34
Location: Aberdeen, UK
I don’t think the scenario you described can be solved directly be just configuration in the maps. If I understand you right you are using composite keys as your identifier. (Primary key composed of several columns in the table)

http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html#mapping-declaration-compositeid

When using composite key, Hibernate can not auto-generate the value for the identifier, which must then be handled in the application.

The problem you are describing is not possible to configure up just out of the box in the maps

The primary key section in the Account Map could look something like:
Code:
<id name="accountId" column="account_id" type="java.lang.Integer">
  <meta attribute="use-in-equals">true</meta>
  <generator class=" sequence "/>
</id>


The primary key section in the Computer Map could look something like:
Code:
<composite-id>
    <key-property name="computerId" column="computer_id" type="java.lang.Integer">
         <meta attribute="use-in-equals">true</meta>
     </key-property>
     <key-property name="accountId" column="account_id" type="java.lang.Integer">
        <meta attribute="use-in-equals">true</meta>
      </key-property>
</composite-id>


Hope this helps!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 01, 2005 10:42 am 
Beginner
Beginner

Joined: Thu Sep 01, 2005 7:43 am
Posts: 31
Location: León (Spain)
Do you really need that connection's id and computer's id can be shared? Otherwise, you only have to map two one-to-many relations and forget about composite keys... You will be able to traverse the object hierarchy either way.

Bye.

_________________
Please rate...

Expert on Hibernate errors... I've had them all... :P


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 01, 2005 1:18 pm 
Newbie

Joined: Wed Aug 31, 2005 11:03 pm
Posts: 2
Hector_Lopez wrote:
Do you really need that connection's id and computer's id can be shared? Otherwise, you only have to map two one-to-many relations and forget about composite keys... You will be able to traverse the object hierarchy either way.


No. I guess my thinking was that I dont want someone (in the application) to be able to reference the machine without also knowing its account number, for security reasons. I guess I could, in theory, handle that at the application level as opposed to the database level... I just figured it would be more efficient to do it at the database level, thats all. Of course, in the real thing I wouldn't be using incremental keys. That was done for illustration purposes only.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.