-->
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.  [ 5 posts ] 
Author Message
 Post subject: Mapping multiple rows to one persistent object?
PostPosted: Wed Nov 02, 2005 12:25 pm 
Newbie

Joined: Wed Nov 02, 2005 12:08 pm
Posts: 3
Hi,
I'm new to hibernate and I've run into a problem trying switch over an old project to use hibernate. In this project we have a table named 'Config' which consists of two varchar rows 'Parameter' and 'Value'. The table simply stores a key and a value for various configuration properties. The problem is that within the system the table is not mapped as a collection of Config items, but instead it is mapped explicitly to properties, eg:

In the Table we have:
-------------------------
(PK) Parameter | Value
-------------------------
key1 | valueOfKey1
key2 | valueOfKey2
...

And in the POJO we have:

Code:
public class Config
{
   private String key1, key2;

   public String getKey1();
   public void setKey1(String value);
   public String getKey2();
   public void setKey2(String value);
}


The values for the Parameter column should never change. Is there anyway in Hibernate to map this?
I know a workaround whereby I map the rows as child elements inside a collection and then explicitly extract the values I need and assign them via the accessors, but I would rather avoid this if I have to in order to keep my data as dumb as possible.

Thanks in advance[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 4:44 pm 
Regular
Regular

Joined: Sun May 08, 2005 2:48 am
Posts: 118
Location: United Kingdom
Why cant you assemble the One object you want with all the variables from a DAO object, your DAO method may look like this:

Code:
MyConfigObject config = new MyConfigObject();

Query q = Session.createQuery("FROM ConfigTable rec");
List list = q.list();
Iterator it = list.iterator();
while(it.hasNext()) {
ConfigTable confRec = (ConfigTable) it.next();
String k = confRec.getKey();
String v = confRec.getValue();

if(k.equals("somevalue"))
  config.setSomeValue(v);
else if(k.equals("othervalue"))
  config.setOtherValue(v);
etc....
}
return config;


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 5:38 pm 
Newbie

Joined: Wed Nov 02, 2005 12:08 pm
Posts: 3
dlmiles wrote:
Why cant you assemble the One object you want with all the variables from a DAO object, your DAO method may look like this:
...


Trust me to not to think about querying for the data! I got so wrapped up in 'having' to use mappings for everything I forgot that I could do more stuff in the DAO. Thanks, this solution would be ideal.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 6:17 am 
Newbie

Joined: Mon Oct 24, 2005 2:51 pm
Posts: 7
Why can't you have a Config class with a Collection of properties.

Code:
public class Config
{
   Set properties;

   public Set getProperties();
   public Iterator getPropertiesIterator();

}

public class ConfigProperty
{
   String key;
   String value;

   public String getKey();
   public String getValue();
}



You could map this type of config. An entity with only a collection.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 6:50 am 
Regular
Regular

Joined: Sun May 08, 2005 2:48 am
Posts: 118
Location: United Kingdom
What would the map look like ? Can you have the collection and class in one table ?

That is the class containing the collection (class Config).
And the class which is the collection (class ConfigValues).

but now you have two classes and two tables, which is not how the data currently exists for this user.


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