• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hibernate: how to create one-column table for set

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class like this:

class RoleList {
private Set<String> roles;
}

I'd like to store this in a table with a single column, but I don't see how to do it. The <set> element in a .hbm.xml file always creates a second table.

Any advice appreciated.

Steven Gollery
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure why you would need the <set /> element here. Set is used to describe an collection of associated objects. Just map your single column table as you might any other table.
 
Steven Gollery
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,

Sorry -- I guess I'm too much of a hibernate newby to understand your answer.

In my model, RoleList is a singleton. I'd like RoleList to map to a table with a single column that contains the values from the roles field, which is a set of strings. If roles was a single string, then your suggestion (as I understand it) would work, but I don't see how to apply that principle to the situation that I'm in.

Would it be possible for you to post a .hbm.xml that might help me to understand what you have in mind?

Thanks,

Steven Gollery
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah - are you saying you have a multi-valued field? Does the field contain something like a CSV list of roles?
 
Steven Gollery
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,

I have a class, RoleList, that has a single data member called roles that is a set of strings (the class definition is in my first message).

If I were managing the JDBC manually, I would create a table named ROLES that has a single column where every row contains a single string which is the name of one of the roles. Something like this:

create table ROLES (role varchar(255) not null);

Then when I instantiate my RoleList object, I would query the db like this:

select * from ROLES;

and iterate over the result list, taking each string and putting it into the set of strings in my RoleList object.

What I'm having trouble with is figuring out how to create a RoleList.hbm.xml file that would allow hibernate to do the same thing. As you point out, using the set element in the hbm.xml file creates an association, and therefore an unnecessary table (as well as requiring an equally pointless key attribute in RoleList to allow the association to work).

Any advice you can give me would be appreciated.

Steven Gollery
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hibernate collections are specifically for associations, not single tables. You can't use Hibernate to map a result set, which is what it sounds like you are trying to do.

You can map roles itself, something like this:


Of course to build your Set or roles you'll have to manually query the contents of the table.
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is "collection of values" what you are looking for ?

http://www.hibernate.org/hib_docs/v3/reference/en/html/tutorial.html#tutorial-associations-valuecollections


pascal
 
Steven Gollery
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pascal,

Yes, a collection of values is what I'm using and it works fine. I found it hard to believe that Hibernate would require me to create a second table that would be unnecessary using straight JDBC, but it looks like that's the case.

Thanks for the help.

Steven Gollery
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic