tags:

views:

128

answers:

6

I want to develop a simple orm which performs CRUD functionality.Shold i use reflection? does libs like hibernate use reflection?? does using reflection will cause the speeed to drop down by a large extent?

A: 

To answer the last part of your question, yes; reflection is a serious performance hit. All the work that you normally have the compiler to you instead have to do at run time, so use reflection sparingly (cache classes for example so you only create them once, preferably at startup).

I haven't looked through Hibernate's code, but I expect it uses reflection as well, but as optimized as possible.

My recommendation is that you write a working dead-simple solution first, then start optimizing as you go along.

mikek
+4  A: 

Yes Hibernate uses reflection and annotations (or XML configuration files), but it will only index and read all meta information once (at startup). I would recommend though to look at the existing ORM solutions first before you start rolling your own.

Daff
Definitely. Reflection is the minor problem you'll face... \\ Saying this as ORM developer.
Alex Yakunin
+2  A: 

A simple ORM is DAO (Data Access Object). You can specify your CRUD operations very well. As mentioned already, Reflection hampers performance. That's the reason why Hibernate went mostly on Annotations for the latest release (to speed up performance).

For More ORM patterns or Methodology, read Martin Fowler's book: Patterns of Enterprise Application Architecture

Also, you can use the existing JPA (Java Persistence API) and write your own JPA.

The Elite Gentleman
Technically, ORM != DAO. Also, annotations aren't a replacement of reflection. Contrary, it requires **more** reflection. Annotations are just a replacement of XML config files. Reflection just became faster due to optimizations in JVM/Hotspot and faster computers through years.
BalusC
Hibernate strongly went with Annotations because it was faster than it's reflection counterpart especially if objects have child nodes that grows exponentially. It was computationally expensive with reflection.
The Elite Gentleman
+2  A: 

Reflection, dynamic proxies, cglib, asm, javassit - all are used in ORM tools.

But, you really don't want to create a new one. Because you can't create a simple ORM. ORMs aren't simple to create and you will realize it once you reach a certain point. So don't waste your time. Use an existing one. There are plenty, some more complicated, some less complicated (and less powerful).

You can google for "simple ORM" and you will have plenty of choices that are (more or less) easy to use. (But not to implement)

Bozho
Don't discourage him, that's how Apache Cayenne and Hibernate got started!
The Elite Gentleman
no offence, but I don't think he can compare to Gaving King. And moreover - it was needed then, it isn't now.
Bozho
A: 

Well, not so long ago, I wrote an ORM layer for GAE named gaedo. This framework is modular enough to also fit relationnal databases. Hopefully, it was my third attempt at such a job. So, here are what is needed and why.

  • Reflection is the root of all ORM amapping tools, since it'll allow you to explore classes looking for their attributes names and values. This is a first use. It will also allow you to load values from your datastore, procided your bean has a convenient constructor (usually, ORM frameworks rely upon Java Beans, since these beans ensure a no-arg constructor exists). Finally, reflection will allow you to load values from datastore in beans, which is, i think, the most important think. Unfortunatly, you'll fast be faced with the issue of the query that loads the whole database, which will require you the two newt steps
  • Considering graph loading, you'll fast need to rely upon dynamic proxies to create lazy loadable objects. Obviously, if you rely solely upon JDK, you will only able to use that on objects implementing well-known interfaces (as an example, collections and maps are very good examples of objects benefiting from dynamic proxies implementing their interface).
  • Finally, annotations will be of smaller use. They'll allow you to define key elements (used to generate the database key for an obejct, as an example), define parent-children relationships, or even define lazy-loading strategy, in association with previously mentionned dynamic proxies.

This is an interesting, but mostly useless, research effort. Interesting, because it will learn you tons of concepts regarding reflection, proxies, and all those things people ignore and tend to consider as reserved to so-called dynamic languages.

But useless, because you'll always encounter corner cases requiring you hacking your code.

As Emmanuel Bernard told in "Les castcodeurs" (a french Java podcast), I think, each year, someone come with a "reimplementation" of Hibernate. And each year, this implementation reveals itself lacking some important fragments, like transaction, local or distributed, cache handling, ...

So, try to code it, and never forget it can be dropped soon due to a too great overlap with installed frameworks.

Riduidel
+1  A: 

Try JLibs-JDBC.

This is simple ORM which doesn't use reflection or xml configuration

Santhosh Kumar T