-->
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.  [ 3 posts ] 
Author Message
 Post subject: Mapping a collection of Integers
PostPosted: Sun Feb 20, 2005 12:56 am 
Newbie

Joined: Wed Aug 25, 2004 11:16 pm
Posts: 12
Location: San Francisco, CA
I'm struggling with a really basic collection mapping. It's a unidirectional one-to-many between an simple class and java.lang.Integer. I don't want to create a wrapper class to represent the Integer.

Context: A BIN is a unqiue identifier. A BIN Group is a named grouping of such identifiers, with no duplicates and in no particular order.

The code looks like this:
Code:
class BinGroup {
    /* Insert basic getters and setters for all of the member vars. */
    private Long id;  // Unique identifier
    private String name;  //  Textual name
    private Set bins;  // Collection of java.lang.Integers
}

Note: I don't really care what Collection type I use here (Set is propbably most appropriate though).

The database tables look like this:
Code:
bin_groups {
  id : bigint(8) # Primary key / Identity
  name : varchar(50) # Not-null
}

bin_group_membership {
  group_id : bigint(8)
  bin : int(4) # Not-null
}

I want to be able to fetch a BinGroup given an id, and have the collection of BINs populated at the same time. When I make changees to the collection of BINs, and save() the BinGroup, it should be updated. The SQL to fetch all of the BINs for a BinGroup looks like "select BIN from bin_group_membership where group_id = ?;"

Mapping would be easy if the contained class wasn't an Integer, but rather, something I've created. I don't want to create a wrapper class though because I don't want to have to add an id column to bin_group_membership (mapping to an existing db).

My mapping thus far looks like:
Code:
<class name="eg.BinGroup" table="bin_groups">
  <id name="id" column="id" type="java.lang.Long">
    <generator class="identity" />
  </id>
  <property name="name" type="java.lang.String" column="bin_group_name" />
  <set name="bins" table="bin_group_membership">
    <key column="group_id" />
    <one-to-many class="java.util.Integer" />
  </set>
</class>

This mapping throws this exception during initialization: net.sf.hibernate.MappingException: Associated class not found

This is such a basic database mapping, I clearly must be missing something... unfortunately, it's not a very easy thing to search/google. Any help or pointers would be appreciated.


Top
 Profile  
 
 Post subject: RE: Mapping a collection of Integers (Solved)
PostPosted: Thu Feb 24, 2005 2:02 pm 
Newbie

Joined: Wed Aug 25, 2004 11:16 pm
Posts: 12
Location: San Francisco, CA
Well, after trying a few solutions that seemed way too complicated (e.g. creating a composite-id combining the group_id and bin columns), I finally found the right way to generate this mapping.

It was embarrassingly simple once I figured it out. I wish it there was a repository of mappings someplace, where people could contriibute example mappings (Java code, table definitions, and then hibernate mapping).

In any case, the mapping that does the trick is:

Code:
<class name="eg.BinGroup" table="bin_groups">
  <id name="id" column="id" type="java.lang.Long">
    <generator class="identity" />
  </id>
  <property name="name" type="java.lang.String" column="bin_group_name" />
  <set name="bins" table="bin_group_membership">
    <key column="group_id" />
    <element column="bin" type="java.lang.Integer"/>
  </set>
</class>


Hopefully this thread will be useful to someone else in the future.

Regards,
Christian


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 24, 2005 3:32 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
If you want to contribute some examples, just do so on the wiki, thats a nice idea :) Everybody can add his stuff once its started up.


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