tags:

views:

1146

answers:

3

I'm currently using ibatis to returns some pojos and everything works great.
My question is: I have to return 1 row from a table, just like 3 fields, and I don't want to create a pojo for it. I just want to run the query and get the 3 values. Is there any easy way to do this without create a special java object just for this?

+4  A: 

in IBatis.NET we use Hashtable when we need more than one value from a query.

<resultMap id="ResultMap1"  class="Hashtable"> 
      <result column="FirstName" property="FirstName" type="string" /> 
          <!-- shows up as "FirstName"  in the Hashtable --> 
      <result column="LastName" property="LastName" type="string" /> 
          <!-- shows up as "LastName"  in the Hashtable --> 
</resultMap>
Binoj Antony
Thats kind of what i guess. Is this just set in the resultClass="HashMap" or something like that? I've used resultset="int", but it only works for one value, and I need three.
Nick
First error was: Attribute "type" must be declared for element type "result". I removed the type="string" from your element to fix that. Second error was Hashtable was not recognized, changed HashTable to "java.util.HashMap" to fix that...
Nick
Now it gives me no errors until I add a select into the mix. Then I get "Could not initialize class. Cause: java.lang.ClassNotFoundException: ResultMap1"[code] <select id="selectData" resultClass="ResultMap1"> SELECT fname, lname FROM operators WHERE username = #login# </select>[/code]
Nick
Ok Binoj, you are the man with the answer!I just made a rookie error and was using resultClass instead of resultMap in my select.
Nick
A: 

I'm not aware of any method for doing what you are asking; the specific purpose of iBATIS is to automate the mapping of relational models to classes.

I'm not sure what you're trying to do, but if you have meaningful data, you should be able to map to an object of some sort, even if the object will be short-lived. I'm guessing that you need some logic based on the values fetched by your query? If that's the case, create a new POJO, map the query to it and then move the logic into your new POJO instead of wherever it is now. This will make the code much cleaner and easier maintain.

If you're just trying to pull back raw data without mapping to a class, you probably need to give your design a second look.

(I know that's not the answer you're looking for . . . sorry.)

Ickster
It wasn't the answer I wanted but thanks.I'm dealing with a legacy database so total design control is not thing to be had.
Nick
For the most part, 99% of the project has or will have pojos mapped from the databse, but given the database there are a lot of bs queries that I'd like to do without creating a lot of bs pojos just for that purpose.
Nick
Glad you got the answer you needed from Binoj Antony. I felt like I was being annoyingly preachy when writing my answer . . . I had started writing 'I can't imagine a case where' and then thought of the legacy code I work on ;-)
Ickster
A: 

You can map the results to a java.util.HashMap (LinkedHashMap if you want to preserve field order).