views:

479

answers:

3

So lets say I have an object that it stored in a Database:

com.test.dummy.Cat

The cat has an integer id.

Outside of my "world" the reference to the object is a String of the form:

Cat-433

Where 433 is the Cats assigned Database ID.

When I get the String passed to me, I need to be able to find it in the Database.

So I do this:

String[] splitString = str.split("-");
String objectType = splitString[0];
Integer id = Integer.valueOf(splitString[1]);

My question is: What would be the best way to get from Cat to com.test.dummy.Cat so I can do a Class.forName() on the String and find that object in the DB?

I was thinking some sort of XML mapping, or hardcoded pure java mapping. What are my options?

+2  A: 

A map, like HashMap. It can be set up in code, or the pair can be read from a file using any of several classes for this purpose (e.g., in Spring).

But in the larger picture, your plan is fragile. You have to parse a string, you have to assume you'll never use a class in another paxckage with the same name, etc.

You probably need to rethink what you're doing, or maybe post your goal, not your implementation, as a question.

Comment from OP:

But since I would control the mapping from the SimpleName to the ClassName, would that really be a problem? – cloutierm (2 mins ago)

In a very simple toy app, this might work. In a real app, you'll find that you're using library classes that have the same simple name.

Or to put it another way, by doing this you're saying, "I'll never use Spring clasess, or Apache Commons classes or even java.lang classes without having to grep through my code looking for name clashes." It's to avoid such inevitable classes that packages were designed.

tpdi
Agree - not storing the package name is going to cause problems
matt b
But since I would control the mapping from the SimpleName to the ClassName, would that really be a problem?
Grasper
That depends on how you want to set up your mappings. You implied that you would use the class name as the key, which of course fails if there is more than one class with that name. If you don't do this, so the second Cat class would have a key of Cat2 (for instance), it is possible.
Robin
Of course, this is a pain to maintain and simply using the fully qualified name would be much simpler. It would also not require any mapping.
Robin
+2  A: 

If you already know that you're going to be reading a Cat from the database, why isn't your DAO for the Cat table coded to create a new Cat() before populating it with the values you read?

You would have a Factory that would return a "DatabaseObject" for the method getDatabaseObject(String reference) that would do the splitting of the "Cat-433" name, use the first part to decide which DAO to call, and the second part to pass in as the UID in the DAO's DatabaseObject get(int uid); method.

JeeBee
+1  A: 

Assuming your types are read in at runtime you could populate the mapping table automatically, i.e.

public class Cat { ... static { mapping.put("Cat", "xyz.Cat"); } ... }