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