> I was refering to the kind of lazy loading that is implemented in
> hibernate where objects members are not queried from the db until they
> are actually read.
Yeah, ORMLite will never support lazy loading. What hibernate is doing to support this is outside any possible definition of the word "lite". However, read on.
> This is the cursor I speak of:
> http://developer.android.com/reference/android/database/Cursor.html
Right. I know about this and ORMLite uses it with the iterator() methods from the DAO. These allow you to page through the items in a big table. It won't do it automatically but by using the Iterator<T> you can get the next X results and put them into your list.
Here's the documentation on the iterator:
http://ormlite.sourceforge.net/javadoc/ormlite-core/doc-files/ormlite_2.html#IDX111
You can do it as a for loop or you can get the iterator directly.
gray
> The BaseAdapter android class needed random access as well as a "size"
> value for the data which iterators do not provide.
Yeah. Sounds like that violates the idea of a cursor at least according to JDBC.
> I have got around
> this problem now by calling getHelper().getReadableDatabase() and
> implementing a query on the database directly.
Sure. Could the DOA help you convert a row into a T type? How about a method that takes a DatabaseResults (which you get in Android land from a Cursor) and returns the current result T? Would that have helped?
> As a feature suggestion for future releases of ormlite for android,
> maybe a method in the dao class to return an android cursor directly
> from a query? Not essential but might make life slightly simpler for
> some people.
We can get close but not quite.
- QueryBuilder.prepare() returns a PreparedQuery<T>
- PreparedQuery.compile(DatabaseConnection conn) returns a CompiledStatement.
- AndroidCompiledStatement has the cursor but it is private. I've made the getCursor() public here but it won't be on the CompiledStatement interface.
All of the levels of iterface and stuff are [unfortunately] necessary to protect the OMRLite core code from the implementation details of JDBC and Android calls.
gray
>> Sure. Could the DOA help you convert a row into a T type? How about a method that takes a DatabaseResults (which you get in Android land from a Cursor) and returns the current result T? Would that have helped?
>
> This would be really helpful for my purposes.
So I added this in v4.3 and refactored it in v4.10.
http://ormlite.com/raw-queries
What it does now is give you a String[] and you return the T. What is still missing is the same thing but as an Object[]. Do you need that functionality?
> After a quick look at the documentation, I couldn't find it.
What sorts of things were you looking under? What terms or concepts?
> I plan to override java.util.List and move the cursor when necessary
> and load the data only if needed. So all the internal stuff is nicely
> hidden in a single class.
This functionality is not already included in ORMLite or are you talking about if you roll your own?
gray
p.s. I'm CC'ing the list because this sounds like stuff other's might be interested in.
> The main problem is that Android needs a functionality to
> jump to an arbitrary location in the list of the results (because I'm
> displaying the results from the database in a ListView). Because
> ORMLite supports only ordered access to the results of a query (over
> the Iterator-interface), the Iterator is no option for me because I
> need random access.
The GenericRawResults can also get the results as a List. This isn't efficient for large numbers of results but I suspect that the ListView isn't made for large numbers.
http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/dao/GenericRawResults.html#getResults%28%29
> For the last step, it would be really helpful if there were a method
> to convert a row which is accessed by an Android-Cursor to T.
Hrm. So the PreparedQuery is also a RowMapper. It has a mapRow(DatabaseResults results) method on it. You should be able to move the cursor, and then pass the results object to mapRow to get the T.
Let me know if that works. A small test program demonstrating that would be a great addition to the Android examples.
gray
>>
> Actually, ListView is intended to handle large numbers of entries
> well, though I don't suppose it often does. The random access is so
> that only the entries actually on the screen have to be kept in
> memory.
The problem that I see is that very few database cursors allow you to go backwards. If they do then I assume that the results are in memory, not being re-read from disk.
> Since I'm up against exactly this problem now, I'll give it a go and
> let you know if I come up with anything useful.
Please do. Best of luck.
gray
> We should figure out what else besides a getCursor() method would be
> useful for an AndroidBaseDao/Impl first. Right now, the single method
> would be nice but that can easily get added to the Android docs since
> not everyone may use it.
I've been following along with this but don't have much to add so I've not been joining in. I'm very happy to add some more base classes that provide a richer feature set for Android folks.
gray