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.  [ 2 posts ] 
Author Message
 Post subject: many-to-many bidirectional mapping with uuids
PostPosted: Tue Mar 22, 2005 6:46 pm 
Newbie

Joined: Thu Feb 17, 2005 11:50 pm
Posts: 4
I've spent all day searching for an example of this, but can't seem to find a solution.
I have recreated the problem in a simpler environment (running with a MySQL db):
2 classes, JitUser and JitGroup.


Code:
package com.digilore.model.bean;

import java.util.HashSet;
import java.util.Set;

/**
* @hibernate.class
*    table="JitGroup"
*/
public class JitGroup {

   private String id = null;
   private String title = null;
   private Set jitUsers = new HashSet();

   public JitGroup() {
      super();
   }

   /**
    * @hibernate.id
    *  column="id"
    *    length="32"
    *  unsaved-value="null"
    *  generator-class="uuid.hex"
    */
   public String getId() {
      return id;
   }


   /**
    * @hibernate.set
    *  table="JitUser_JitGroup"
    *  cascade="none"
    *  lazy="false"
    *  inverse="true"
    * @hibernate.collection-key
    *  column="jitGroup"
    * @hibernate.collection-many-to-many
    *  column="jitUser"
    *  class="com.digilore.model.bean.JitUser"
    */
   public Set getJitUsers() {
      return jitUsers;
   }

   /**
    * @hibernate.property
    *  column="title"
    */
   public String getTitle() {
      return title;
   }

   public void setId(String id) {
      this.id = id;
   }

   public void setJitUsers(Set jitUsers) {
      this.jitUsers = jitUsers;
   }

   public void addJitUser(JitUser jitUser) {
      jitUsers.add(jitUser);
      jitUser.getJitGroups().add(this);
   }

   public void removeJitUser(JitUser jitUser) {
      jitUsers.remove(jitUser);
      jitUser.getJitGroups().remove(this);
   }

   public void setTitle(String title) {
      this.title = title;
   }

}


Code:

package com.digilore.model.bean;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
* @hibernate.class
*    table="JitUser"
*/
public class JitUser {

   private String id = null;
   private String userName = null;
   private Set jitGroups = new HashSet();

   public JitUser() {
      super();
   }

   /**
    * @hibernate.id
    *  column="id"
    *    length="32"
    *  unsaved-value="null"
    *  generator-class="uuid.hex"
    */
   public String getId() {
      return id;
   }

   /**
    * @hibernate.set
    *  table="JitUser_JitGroup"
    *  cascade="none"
    *  lazy="false"
    *  inverse="false"
    * @hibernate.collection-key
    *  column="jitUser"
    * @hibernate.collection-many-to-many
    *  column="jitGroup"
    *  class="com.digilore.model.bean.JitGroup"
    */
   public Set getJitGroups() {
      return jitGroups;
   }

   /**
    * @hibernate.property
    *  column="userName"
    */
   public String getUserName() {
      return userName;
   }

   public void setId(String id) {
      this.id = id;
   }

   public void setJitGroups(Set jitGroups) {
      this.jitGroups = jitGroups;
   }

   public void addJitGroup(JitGroup jitGroup) {
      jitGroups.add(jitGroup);
      jitGroup.getJitUsers().add(this);
   }

   public void removeJitGroup(JitGroup jitGroup) {
      jitGroups.remove(jitGroup);
      jitGroup.getJitUsers().remove(this);
   }

   public void setUserName(String userName) {
      this.userName = userName;
   }

}



Forgive me for putting my question in terms of xdoclet tags, but that's what I'm familiar with.
When I attempt to schemaexport on the generated hbm.xml's, I get the following error:
[schemaexport] create table JitUser_JitGroup (
[schemaexport] jitGroup varchar(255) not null,
[schemaexport] jitUser varchar(255) not null,
[schemaexport] primary key (jitUser, jitGroup)
[schemaexport] );
[schemaexport] (hbm2ddl.SchemaExport 154 ) Unsuccessful: create table JitUser_JitGroup (jitGroup varchar(255) not null, jitUser varchar(255) not null, primary key (jitUser, jitGroup))
[schemaexport] (hbm2ddl.SchemaExport 155 ) Invalid argument value, message from server: "Specified key was too long. Max key length is 500"

I don't understand why each column in JitUser_JitGroup is a varchar(255).
If it helps in identifying the problem, this only occurrs when I specify inverse="true" for either side.
That is, when both sides of the relationship specify inverse="false", the problem does not occur (I am aware of the run-time problem this incurs).
When inverse="false" for both sides, JitUser_JitGroup has two fields, each of which is length=32 and everything appears correct, except for the aforementioned run-time problem.

What am I doing wrong?
This can be done, right?

Any help would be appreciated.
Thanks in advance
-Trey


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 23, 2005 11:48 am 
Newbie

Joined: Thu Feb 17, 2005 11:50 pm
Posts: 4
After tinkering with this scenario a bit more, I discovered that if I specify the @hibernate.collection-key-column name="<col_name>" length="32" right after the @hibernate.collection-key, then the JitUser_JitGroup table is generated with the appropriate columns.

Still not sure why the length isn't 32 when I specify inverse="true", but since this fixes it, I'm moving on.

I hope this is helps someone in the future.

-Trey


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