database content as objects

  • Thread starter =?ISO-8859-2?Q?Dra=BEen_Gemi=E6?=
  • Start date
?

=?ISO-8859-2?Q?Dra=BEen_Gemi=E6?=

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
 
C

Chris Smith

Dra¸en Gemic said:
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.

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).

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
?

=?ISO-8859-2?Q?Dra=BEen_Gemi=E6?=

Chris said:
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.

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
 
C

Chris Smith

Just to argue the other side,

Dra¸en Gemic said:
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.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
J

Jeffrey H. Coffield

Dra¾en Gemiæ said:
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
A HashMap is an in-memory structure?

What if your database contains 10 to 100 million records?

What about concurrent multi-user updates?

Jeff
 
?

=?ISO-8859-2?Q?Dra=BEen_Gemi=E6?=

Chris said:
Just to argue the other side,




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.

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
 
S

steve

No.

1 record = 1 HashMap object

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
 
?

=?ISO-8859-2?Q?Dra=BEen_Gemi=E6?=

steve said:
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
 
J

Jeffrey H. Coffield

Dra¾en Gemiæ said:
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

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
 
O

Oliver Wong

Jeffrey H. Coffield said:
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.

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
 
S

steve

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

Yep, ok whatever man.
the advice is free, obviously the only person on the planet to have thought
of this.

Steve
 
S

steve

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top