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: update join table
PostPosted: Thu Aug 17, 2006 5:55 pm 
Newbie

Joined: Thu Aug 17, 2006 5:43 pm
Posts: 2
Dear all.

I am quite a hibernate beginner, so please excuse if my question seems simple.
I have the following mapping:

Code:
<class name="User" table="users">
   <id name="uid" column="uid">
      <generator class="native" />
   </id>
   <join table="server">
      <key column="uid" unique="true" />
      <many-to-one name="server"
         class="ServerName" column="serverId" not-null="true" />
   </join>
</class>
<class name="ServerName" table="serverNames">
   <id name="serverId" column="serverId">
      <generator class="native" />
   </id>
</class>


So I have three tables:
Code:
table users ( uid bigint not null primary key )
table server ( uid bigint not null primary key, serverId bigint not null )
table serverNames ( serverId bigint not null primary key )


All works fine, I can instantiate the appropriate classes, no problems so far. But now I have some understanding problem. How can I update the table "server", i.e. I want to asign another serverId to a given uid, i.e. I want to update the join table... how can I access the join table and update it via hibernate?

How you can help me, thanks in advanced,

greetings,

Norman[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 17, 2006 7:51 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
With the mapping you have, you'd use user.setServer(server). However, I think it unlikely that your mapping is correct. You've said that a User is made up of data from two tables, users and server. I think it's more likely that User is made up of data only from the users table, and a User has an associated server. It's possible that a server is made up of data from two tables, server and serverNames. Though if a server can have more than one name, then it would be more correct to map this as a server containing a collection of names. Finally, guessing from the shape of the tables, and the fact that you've called server a join table, I'm going to say that you actually want a many-to-many mapping between users and serverNames.

This is probably the mapping that you're looking for:
Code:
<class name="User" table="users">
   <id name="uid" column="uid">
      <generator class="native" />
   </id>
   <set name="servers" table="server">
     <key column="uid" not-null="true">
     <many-to-many class="ServerName" column="serverId"/>
   </set>
</class>

<class name="Server" table="serverName">
  <id name="serverId" column="serverId">
   ...
  </id>
  <set name="users" table="server" inverse="true">
     <key column="serverId" not-null="true"/>
     <many-to-many class="User" column="uid"/>
  </set>
</class>
If you actually want a one-to-many relationship from serverName to users, but with a join table (server), then you can convert this mapping with help from the ref docs section 7.5.1.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 21, 2006 2:22 pm 
Newbie

Joined: Thu Aug 17, 2006 5:43 pm
Posts: 2
Thanks for your help, you set me on the right track! But how would I solve this if I don't want to have a many-to-many relationship from User to Server. Say my classes look like this:

Code:
class User {
   Long uid;
   Server server;
}
class Server {
  Long serverId;
  String serverName;
}

So I don't want to use a set to store the association between user and server. This is because each user must have only one server. Is one-to-one the right decision? I am rather stuck here.

Thanks again for your help

greetings

norman


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 21, 2006 10:15 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Not one-to-one, probably. Unidirectional many-to-one would be better. Or even bidirectional one-to-many, if you want servers to know what users use them. Then you could have
Code:
class Server {
  Long serverId;
  String serverName
  Set<User> currentUsers;
}
Whether or not you want servers to know about users, you almost certainly want a many-to-one from User to Server.

_________________
Code tags are your friend. Know them and use them.


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.