-->
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.  [ 6 posts ] 
Author Message
 Post subject: Only 1 item in One-to-Many Set returned
PostPosted: Thu Nov 06, 2003 1:14 pm 
Newbie

Joined: Thu Nov 06, 2003 12:38 pm
Posts: 3
I'm trying to learn hibernate with this simple example. I have User, UserRole and Role tables. A User can have many Roles and many Roles can be assigned to a User.

This piece of code returns the populated UserBean. The problem is it only returns 1 Role in the Set. How do I make it populate all Roles for the User? Am I over simplfying it? What am I missing?

-John

Code:
   public UserBean getUser(String name) throws HibernateException
   {
      UserBean user = (UserBean) _hibernateSession.load(UserBean.class, name);
      
      _hibernateSession.close();
      _factory.close();
      return user;
   }


Code:
public class UserBean
{
   private String _name;
   private String _password;
   private Set _roles;

   /**
    * Constructor for UserBean.
    */
   public UserBean()
   {
      super();
   }
   public void setName(String name)
   {
      _name = name;
   }
   public void setPassword(String password)
   {
      _password = password;
   }
   
   public String getName()
   {
      return _name;
   }
   
   public String getPassword()
   {
      return _password;
   }
   
   public void setRoles(Set roles)
   {
      _roles = roles;
   }
   
   public Set getRoles()
   {
      return _roles;
   }
   
   public Iterator getIterator()
   {
      return _roles.iterator();
   }

}


Code:
# Host: localhost
# Database: authority
# Table: 'roles'
#
CREATE TABLE `roles` (
  `rolename` varchar(20) NOT NULL default '',
  PRIMARY KEY  (`rolename`)
) TYPE=MyISAM;

# Host: localhost
# Database: authority
# Table: 'user_roles'
#
CREATE TABLE `user_roles` (
  `username` varchar(20) NOT NULL default '',
  `rolename` varchar(20) NOT NULL default '',
  PRIMARY KEY  (`username`,`rolename`)
) TYPE=MyISAM;

# Host: localhost
# Database: authority
# Table: 'users'
#
CREATE TABLE `users` (
  `username` varchar(20) NOT NULL default '',
  `password` varchar(20) NOT NULL default '',
  PRIMARY KEY  (`username`)
) TYPE=MyISAM;



Code:
<hibernate-mapping>

   <class name="UserBean" table="users">
      <id name="name" type="string" unsaved-value="null" >
         <column name="username" sql-type="varchar(20)" not-null="true"/>
         <generator class="assigned"/>
      </id>
      <property name="password">
         <column name="password" sql-type="varchar(20)" not-null="true"/>
      </property>
      <set name="roles" lazy="false" table="user_roles" cascade="all">
         <key column="username"/>
         <one-to-many column_id="username" class="com.tyson.struts.beans.UserRoleBean"/>
      </set>
   </class>

   <class name="RoleBean" table="roles">
      <id name="name" type="string" unsaved-value="null" >
         <column name="rolename" sql-type="varchar(20)" not-null="true"/>
         <generator class="assigned"/>
      </id>
   </class>

   <class name="UserRoleBean" table="user_roles">
      <id name="user" type="string" unsaved-value="null">
         <column name="username" sql-type="varchar(20)" not-null="true"/>
         <generator class="assigned"/>
      </id>
      <property name="role">
         <column name="rolename" sql-type="varchar(20)" not-null="true"/>
      </property>
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 06, 2003 1:59 pm 
Regular
Regular

Joined: Tue Sep 16, 2003 11:35 am
Posts: 93
Location: San Francisco, CA
you need a many-to-many relationship if you use a join table


Top
 Profile  
 
 Post subject: Does Hibernate support multiple <id> tags in a <cla
PostPosted: Thu Nov 06, 2003 7:01 pm 
Newbie

Joined: Thu Nov 06, 2003 12:38 pm
Posts: 3
Hmm. Sorry, but the many-to-many doesn't work. I just get a bunch of errors that I really didn't want to track down and it isn't the correct relational mapping anyway.

I finally got it to work (sort of) by adding multiple <id> tags in the UserRoleBean class. However, when I listed <id name="userName"...> first it doesn't get set by reflection. Conversely, if I switch the two and put <id name="roleName"...> first, then the roleName doesn't get set in the object. :(

Code:
   <class name="UserRoleBean" table="user_roles">
      <id name="userName" type="string" unsaved-value="null">
         <column name="username" sql-type="varchar(20)" not-null="true"/>
         <generator class="assigned"/>
         <many-to-one name="username" class="com.tyson.struts.beans.UserBean"/>
      </id>
      <id name="roleName" type="string" unsaved-value="null">
         <column name="rolename" sql-type="varchar(20)" not-null="true"/>
         <generator class="assigned"/>
         <many-to-one name="rolename" class="com.tyson.struts.beans.RoleBean"/>
      </id>
   </class>


Being adventurous I added the entry below for the UserRoleBean and now the userName gets set! :S

Code:
      <property name="userName">
         <column name="username" sql-type="varchar(20)" not-null="true" update="false" insert="false"/>
      </property>


It couldn't be that easy though. With this configuraton I can see users and the roles assigned to them. But, I can't see the users assigned to roles. For instance, I have 4 roles (owner, manager, waitress, cook) and some users (joe, bob, jane). The database is set up as... Joe is the owner and a cook. All users are cooks.

If I ask for all users assigned to the role 'cook' the result is:

Code:
User   Role
joe   cook
joe   cook
joe   cook

But if I ask for roles for user 'joe' the result is:
Code:
User   Role
joe   cook
joe   owner


Does Hibernate support multiple <id> tags in a <class> definition? I couldn't get a clear understanding from the documentation. If it does, am I doing it correctly? -John


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 06, 2003 8:15 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Lot's of problems here:
* Not multiple id allowed
* no need to <set table="blah"> in a one-to-many since Hibernate can gess it from the one-to-many class attribute.
* no many-to-one inside id tag
* ...


Listen to cutie, Many-to-many IS what you want

Try someting like that

Code:
<class name="UserBean" table="users">
  ...
  <set name="roles" lazy="false" table="user_roles" cascade="all">
         <key column="username"/>
         <many-to-many column="rolename" class="com.tyson.struts.beans.RoleBean"/>
   </set>
   


And read section 5.3 of the reference guide

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 06, 2003 8:53 pm 
Newbie

Joined: Thu Nov 06, 2003 12:38 pm
Posts: 3
I read the entire online documentation. Is there more? I ask because section 5.3 of the online reference doesn't say much in *newbie* talk. For experts it might say a lot but all I see are examples with "one-liners". Its not much help.
I'll add the many-to-many relationship and post the errors it generates. Is there an online help for errors Hibernate generates?

-john


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 07, 2003 5:47 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I can undersand that Hibernate doc may be very obscure for newbies because it assumes you're aware of O/R concepts, but /almost/ everything is in (I know nothing on O/R concepts but the one I learned from it - I read and read it again ;-)).

You can have a look at
http://www.hibernate.org/Documentation/ExternalDocumentation, it points to some good tutorials and samples (I like Scott Ambler's article).

_________________
Emmanuel


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