Mapping Suggestion needed

Posted on 2011-03-16
Medium Priority
Last Modified: 2012-05-11
I have a table which contains 3 columns id,key,value. The data in the table looks like this :-

3)1, lastName, foo Test

Now when i query the table  :-

List<Property> values = hibernateDao.findById("xx");

I get list of values as i mentioned above. I can do this :-

User user = new User();
foreach(Property prop : values ) {
if(prop.getKey().equalsIgnoreCase("userId") {
if(prop.getKey().equalsIgnoreCase("firstName") {
return user;

This works fine but i am trying to find more efficient way of doing it instead of using nested if statements.
Question by:Micheal_Male
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
LVL 47

Expert Comment

ID: 35153023
And what is Property ?
Is it a type defined by Hibernate ?

Author Comment

ID: 35153107
Its an entity class
LVL 47

Expert Comment

ID: 35153116
But who defines it? Does it implement Comparable?
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more


Author Comment

ID: 35153146
It's just a class itself which holds field names id,key and value. It does not implement comparable.

Author Comment

ID: 35153158

public class Property {

private String id;
private String key;
private String value;

Open in new window

LVL 47

Expert Comment

ID: 35153164

If you defined it in your code, then you can implement Comparable
and define compareTo method
in such a way that Collections.sort(values) will produce the known order of keys,
then you would not need to check with if's but could know that fiirst element corresponds
say to userid, second to first name, etc


Author Comment

ID: 35153170
hmm that sounds a little dangerous if someone else adds a new key or it might break by putting the wrong key value in user pojo.
LVL 47

Expert Comment

ID: 35153286
That is true.
If I were you, I would go for if's, as you mentioned in your original post; don't see any problems with those.
I just suggested, as you were looking for possible alternatives.  

Expert Comment

ID: 35158441
what about refactoring your code with the Visitor design pattern?

Accepted Solution

josephtsang earned 1000 total points
ID: 35162738
Work in this way:

1. Replace all lines inside the for-loop by these 2 lines

Setter<User> setter = setterMap.get(prop.getKey());
setter.set(user, prop.getValue())

Open in new window

2. Before entering the for-loop, prepare a setter map as follows

Map<String,Setter<User>> setterMap = new HashMap<String,Setter<User>>();
setterMap.put( "userId", new Setter<User> { 
	public void set(User user, String value) { user.setUserId(value); } });
setterMap.put( "firstName", new Setter<User> { 
	public void set(User user, String value) { user.setFirstName(value); } });
setterMap.put( "lastName", new Setter<User> { 
	public void set(User user, String value) { user.setLastId(value); } });

Open in new window

3. Define a private inner interface Setter in your class

private interface Setter<T> { 
	public void set(T t, String value); 

Open in new window

So if you have defined any new key-value pairs for the records, you just need to put a new entry into the map in Step 2. Others just keep the same. Less code change for the enhancement.

Author Comment

ID: 35162747
Great. However I just found a mapping framework also. Hopefully that will suffix my needs.

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question