aspose file tools*
The moose likes Object Relational Mapping and the fly likes hibernate: how to create one-column table for set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate: how to create one-column table for set" Watch "hibernate: how to create one-column table for set" New topic
Author

hibernate: how to create one-column table for set

Steven Gollery
Greenhorn

Joined: Nov 09, 2006
Posts: 18
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
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Steven Gollery
Greenhorn

Joined: Nov 09, 2006
Posts: 18
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

Joined: Apr 14, 2004
Posts: 10336

Ah - are you saying you have a multi-valued field? Does the field contain something like a CSV list of roles?
Steven Gollery
Greenhorn

Joined: Nov 09, 2006
Posts: 18
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

Joined: Apr 14, 2004
Posts: 10336

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.
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
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

Joined: Nov 09, 2006
Posts: 18
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
 
jQuery in Action, 2nd edition
 
subject: hibernate: how to create one-column table for set