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
Solved

Mapping Suggestion needed

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

1)1,userId,test
2)1,firstName,foo
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") {
user.setUserId(prop.getValue()):
}
if(prop.getKey().equalsIgnoreCase("firstName") {
user.setFirstName(prop.getValue());
}
.
.
}
return user;

This works fine but i am trying to find more efficient way of doing it instead of using nested if statements.
0
Comment
Question by:Micheal_Male
  • 5
  • 4
  • 2
11 Comments
 
LVL 47

Expert Comment

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

Author Comment

by:Micheal_Male
ID: 35153107
Its an entity class
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35153116
But who defines it? Does it implement Comparable?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

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

Author Comment

by:Micheal_Male
ID: 35153158

@Entity
@Table(name="property")
public class Property {

@OneToMany(mappedBy="property")
@JoinColumn("id")
private String id;
@Column(name="key")
private String key;
@Column(name="value")
private String value;

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
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

0
 

Author Comment

by:Micheal_Male
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.
0
 
LVL 47

Expert Comment

by:for_yan
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.  
0
 
LVL 5

Expert Comment

by:josephtsang
ID: 35158441
what about refactoring your code with the Visitor design pattern?
0
 
LVL 5

Accepted Solution

by:
josephtsang earned 250 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.
0
 

Author Comment

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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 …

829 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