Java Forum / General / May 2006
database content as objects
Dražen Gemić - 27 May 2006 16:07 GMT I wonder what is so appealing in mapping of database content to predefined Java (or some other language) objects.
There is a problem with joins in he first place. There is lot of coding and processing overhead.
I found it more elgant to store data in HashMaps. If I need an object, I could always provide a class with a constructor that takes HashMap as parameter.
Reporting can be much easier that way, too. I used it in a couple of projects, and find it very handy.
DG
Chris Smith - 27 May 2006 20:17 GMT Dražen Gemic <usenet@local.machine> wrote:
> I wonder what is so appealing in mapping of database content to > predefined Java (or some other language) objects. [quoted text clipped - 8 lines] > Reporting can be much easier that way, too. I used it in a couple of > projects, and find it very handy. Do you mean that you get data out of the database and store it in HashMaps? The appealing thing about objects would then be:
1. Objects in a statically checked language are easier and safer to use, since you don't face the possibility of mistyping a field name, etc.
2. Development tools have nifty things like auto-complete of method names, which don't work for string keys in hash codes.
3. Objects own their own behavior. Languages implement polymorphic method dispatch. Since functions are not first-class objects in Java, this is too awkward to be feasible for your HashMap approach.
4. Performance of code generated by the JIT compiler is significantly higher (by several order of magnitude, I'd guess) than HashMap lookups on arbitrary strings.
5. There really isn't a whole lot of code to write, anyway. Google Hibernate.
Essentially, you are re-inventing a very poor version of object- relational mapping, with HashMap for your objects and no handling of associations (which is the main benefit of ORM products anyway).
 Signature www.designacourse.com The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation
Dražen Gemić - 27 May 2006 21:22 GMT > Dražen Gemic <usenet@local.machine> wrote: > [quoted text clipped - 23 lines] > method dispatch. Since functions are not first-class objects in Java, > this is too awkward to be feasible for your HashMap approach. Answer to 1, 2, 3 is that I can still have the classes with constructors taking HashMap as a parameter, when and only when I need them
> 4. Performance of code generated by the JIT compiler is significantly > higher (by several order of magnitude, I'd guess) than HashMap lookups > on arbitrary strings. This is irrelevant, since Hashmaps are used to store and retrieve fro database, and that is quite slow anyway.
> 5. There really isn't a whole lot of code to write, anyway. Google > Hibernate. I know about Hibernate, it is not fast, still too much code and config to write. With my own DB library, founded on HashMap-records, I can do database I/O in a few lines of code, just like PHP people do.
DG
> Essentially, you are re-inventing a very poor version of object- > relational mapping, with HashMap for your objects and no handling of > associations (which is the main benefit of ORM products anyway). Chris Smith - 27 May 2006 23:29 GMT Just to argue the other side,
Dražen Gemic <usenet@local.machine> wrote:
> Answer to 1, 2, 3 is that I can still have the classes with constructors > taking HashMap as a parameter, when and only when I need them Right, so you write a class, AND code to retrieve database fields and store them in the HashMap, AND code to retrieve values from the HashMap and store them in an object. Then you write half your code to use HashMap and the other half to use objects. And this is less code than using an ORM product? No, it's clearly not. What's more, it implies that you're writing a considerable portion of your code in a procedural style, which probably has to do with the desire to avoid writing the code that you'd get for free with an ORM product.
You're also ignoring the remainder of ORM. You haven't addressed how to model associations of data, choose between lazy and eager loading of associations, enforce pre-loading of lazy association fields prior to disconnecting from the database, express searches and search criteria. Surely you could solve those problems, but you'd surely end up writing a good bit more code that is reasonable for someone to maintain just to write a bit of database code.
> This is irrelevant, since Hashmaps are used to store and retrieve fro > database, and that is quite slow anyway. This depends on the application.
Among other projects, one of the things I'm doing for work involves using a database to retrieve data that's used in a kind of analysis that takes far more time in computation than the database takes to retrieve information. In fact, the computation is so long that we limit the amount of time spent on it and apply heuristics to finish with a reasonable answer when the time limit is up. The more time is spent comparing strings in the HashMap, the less time is spent analyzing the data.
(In case you're curious, it has to do with analyzing large numbers of something like journal entries and trying to identify different entries that use the same vocabulary in as close as possible to the same grammatical forms; except that grammatical forms is defined in a way that's defined recursively so that it also applies to paragraph structure, etc. The point is, the more computation time, the better the results. We cache only a certain amount of the text in memory at once, but enough that we never have the computation threads waiting on the thread that retrieves new data; they've always got other things they can do.)
This may be a case in which you'd build classes... so fine, but you've now eliminated this application from places where it makes sense to use your HashMap approach. There are surely others like it. The entire world doesn't consist of writing CRUD web apps. If all you do is write CRUD web apps, then Google -> "Ruby on Rails", except that then you might soon be out of a job.
> I know about Hibernate, it is not fast, still too much code and config > to write. Hold on... you write config, OR you write annotated code. Not both. The only time you'd write both is if you were trying to fit an existing database schema to an existing class model, in which case your HashMap idea is clearly useless anyway.
 Signature www.designacourse.com The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation
Dražen Gemić - 28 May 2006 17:51 GMT > Just to argue the other side, > [quoted text clipped - 8 lines] > HashMap and the other half to use objects. And this is less code than > using an ORM product? No, it's clearly not. No. The point is that I have general wrapper classes that work for all the databases, database structures, and queries. They have been written, placed in a package and forgotten. I don't even remember clearly how it works. If somebody asked me I'll have to look at the code. Now they are just imported with import statement.
They are combined with database abstraction layer classes.
I looks like this:
AdbWrapper.init("postgres",username,password,jdbcUrl); AdbWrapper dbw=AdbWrapper.connect(); AdbRecord rec; Date d; Iterator it=dbw.query("select * from mydata inner join otherdata on mydata.x = otherdata.y order by size"); while(it.hasNext()) { rec=(AdbRecord) (it.next()); d=rec.getDate("last_time"); // .... do something with data dbw.updateWhere("mydata","id",rec.getInteger("id"),rec); } dbw.close();
AdbRecord is derived from HashMap.
DG
Jeffrey H. Coffield - 28 May 2006 17:03 GMT > I wonder what is so appealing in mapping of database content to > predefined Java (or some other language) objects. [quoted text clipped - 10 lines] > > DG A HashMap is an in-memory structure?
What if your database contains 10 to 100 million records?
What about concurrent multi-user updates?
Jeff
Dražen Gemić - 28 May 2006 17:21 GMT > What if your database contains 10 to 100 million records? No.
1 record = 1 HashMap object
DG
steve - 28 May 2006 22:49 GMT On Mon, 29 May 2006 00:21:37 +0800, Dra¾en Gemiæ wrote (in article <e5ci1f$bup$1@magcargo.vodatel.hr>):
>> What if your database contains 10 to 100 million records? > [quoted text clipped - 3 lines] > > DG you are really going to have some fun later ;-), sticking stuff into a database as an hashmap object is akin to encryption.
it's not searchable, structure is fixed, unless you de-serialize all your records, change their structure & re-serialize, and put them back into the database.
also you cannot partition your database easily.
Steve
Dražen Gemić - 29 May 2006 01:21 GMT > you are really going to have some fun later ;-), sticking stuff into a > database as an hashmap object is akin to encryption. No, I am not. I can build an insert or update statement using HashMap elements and keys.
The important thing is that I can (I already have) build a framework that is completely general and independent of database structure.
DG
Jeffrey H. Coffield - 29 May 2006 16:27 GMT >> you are really going to have some fun later ;-), sticking stuff into a >> database as an hashmap object is akin to encryption. [quoted text clipped - 6 lines] > > DG I have worked with a database that used hash maps as a way to store the data and that database did not scale well but perhaps I just don't understand exactly what you have programmed.
You say you are not using the hashmap to load the entire database so I assume you use the hashmap somehow to store each record. I don't see what good that does for you.
I am working on a Java interface to a non-relational database and am currently working through the whole issue of locking so I am always looking to see how others have approached database access.
Jeff
Oliver Wong - 29 May 2006 17:53 GMT >>> you are really going to have some fun later ;-), sticking stuff into a >>> database as an hashmap object is akin to encryption. [quoted text clipped - 18 lines] > currently working through the whole issue of locking so I am always > looking to see how others have approached database access. If I understand the OP correctly, the hashmap acts as an associative array or dictionary. Each hashmap represents on row or record in the DB.
When you perform a query against a DB, you get an iterator. You use the iterator to get each hashmap representing on row. The keys of the hashmap are the column names, and the values are the value of that row-column combination.
IOW, it's just like how it works in PHP.
- Oliver
steve - 29 May 2006 23:03 GMT > Dra¾en Gemiæ wrote: >> [quoted text clipped - 22 lines] > > Jeff what ever way it is done, Hashmaps are not the way to go. They are finite, and when you reach the hash limit , you have to do a re-hash. That is before you even consider, a database that might have to partition & scale, or god help him , distribute to remote sites. Subjects such as locking , can only be handled inside the database, and even so you still have to be very careful how you construct your queries & updates. Iv'e seen people take a whole database down, because they were going for lunch , in the middle of a transaction and did not commit before leaving.
Steve
steve - 29 May 2006 22:55 GMT On Mon, 29 May 2006 08:22:15 +0800, Dra¾en Gemiæ wrote (in article <e5de6l$df2$1@magcargo.vodatel.hr>):
>> you are really going to have some fun later ;-), sticking stuff into a >> database as an hashmap object is akin to encryption. [quoted text clipped - 6 lines] > > DG Yep, ok whatever man. the advice is free, obviously the only person on the planet to have thought of this.
Steve
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|