-->
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: (Diligent) Newbie Q: Strings in Lookup tables?
PostPosted: Thu Dec 29, 2005 3:20 pm 
Beginner
Beginner

Joined: Thu Dec 29, 2005 12:08 pm
Posts: 31
Location: Acton, MA
This is a very basic newbie question, and I'm sure the answer may be found somewhere in the piles of excellent documentation. I am happy to read; I just need a pointer. Thanks in advance for anyone's time who is still with me on this. :-)

As I said, a very simple problem. I have a domain object that can take in a String. I'd like, however, on the database side to enforce that the supplied String be present in a lookup/type table. This usually means that the thing I would store on the main table would be an integer pointing at an entry in a lookup table, whose primary key would be the integer and whose value would be the String.

So, for example, instead of storing "state" as a String on my Address table, I might store 2, which would point at the tuple in the State table of {2, Massachusetts}.

Now, these are database semantics. My class' callers are simply going to expect a setState(String) method that may puke if it gets a bad state.

What is the preferred Hibernate mechanism for mapping an arbitrary string property as a pointer-into-a-type-table property instead?

Thanks again; I'll continue reading.

Best,
Laird


Top
 Profile  
 
 Post subject: many-to-one
PostPosted: Thu Dec 29, 2005 4:27 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Define the lookup table as a normal class with an ID and a single string property, and give the class the attribute mutable="false" (allowing hibernate to optimise a few things, and ensure that you don't modify the table accidentally). Use normal many-to-one mappings from other classes to the lookup table. This is fairly normal: the main app where I work has dozens of "type" lookup tables, mostly just an id and string.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 29, 2005 4:59 pm 
Beginner
Beginner

Joined: Thu Dec 29, 2005 12:08 pm
Posts: 31
Location: Acton, MA
Thanks, tenwit; I rated your post as not helpful by mistake--that was not my intention, as it was helpful, but I have one further question.

Suppose I do this. Wouldn't my "main" class (the one referring to the lookup table) still have to have an int type instead of a String type for the property in question?

I guess what I'm saying is I don't want to have to map two classes (the "main" one and Whatever One Represents The Lookup Table)--I don't want there to be two classes at all, or at least not two public ones. I want my API to be in terms of Strings that are either valid or not, and are validated against the lookup table.

Let me put it this way: if there were no database involved at all, then I'd have a setState(String) method that would simply see if the supplied String was in some user-editable list of "good" Strings somewhere, and if validation passed, I'd perform the operation. Is there some way to preserve this API but have the validation implemented at the database level via Hibernate?

I'm smelling something like a PersistentEnum in the mix here, but these aren't exactly enums....

Thanks again for your help,
Laird


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.