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: Storing random object types in same table?
PostPosted: Thu Jun 04, 2009 2:56 pm 
Beginner
Beginner

Joined: Mon Mar 07, 2005 6:23 pm
Posts: 21
I've got a situation where I'd like to store objects of different types (strings, numbers, dates, booleans, etc) in the same table, with a single insert/retrieval point ("getValue()?) on the same mapped class.

I don't really want to use a byte array, since I'd prefer if these values, once stored in the database, were human readable (unless it's a complex object of some sort).

Any recommendations on going about doing this?


Top
 Profile  
 
 Post subject: Re: Storing random object types in same table?
PostPosted: Thu Jun 04, 2009 3:29 pm 
Newbie

Joined: Thu Jun 04, 2009 12:21 pm
Posts: 2
Clay wrote:
I've got a situation where I'd like to store objects of different types (strings, numbers, dates, booleans, etc) in the same table, with a single insert/retrieval point ("getValue()?) on the same mapped class.

I don't really want to use a byte array, since I'd prefer if these values, once stored in the database, were human readable (unless it's a complex object of some sort).


If you want to store all those types in one table (and presumably in one field), then the only way I can think of is to store them in a string column. However, string/varchar is not very efficient when it comes to storing anything that isn't a string.

A different option would be to use inheritance, and map different types to different tables. Perhaps a parent class StoredObject, with method "Object getValue()". Subclasses can override getValue(). Consider a subclass StoredBoolean, with method "Boolean getValue()".

Using this approach, you could retrieve a list of StoredObjects, and each call of getValue() would return the type used by the method in the subclass. If this sounds like a good option for you, read up on Hibernate's inheritance capabilities.


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.