Lately I've been working quite hard on mapping the relationships between tables.
It seems to be a lot harder than I first thought, both because of the complexity of the task in itself and because of another limitation of Java.
Mantaining the relationships is hard because an ORM must, for every change made to an object, change the table directly mapped by that object and all the related tables (updating foreign keys or deleting children rows in varoius tables). Furthermore, when linking mapping objects together, the ORM must take care to update the corresponding tables only when and if it is necesary. Finding out when and which updates must be performed in an intelligent way is not always so obvious.
Another unforeseen problem is Java's inability to dynamically create fields. To be fair, this is a problem most compiled (or byte-code compiled) languages have.
In a scripting language (think about JavaScript or Ruby), you can dinamically add a field (or a method) to a class at runtime. This possibility gives an ORM a great deal of freedom and greatly improves the simplicity and terseness of its interfaces.
In Rails if a table A has a relationship with table B, The class representing table A will have a field b corresponding to an object of type B (or vice versa, depending on the relationship between the tables). This field does not need to be hard-coded into the class, it is dinamically inferred and added to the class, and that field can be used directly (i.e. a.b = "something" if a is of type A).
In Java this is not possible. Java does not permit a field to be added dinamically and this forces the design of the classes to be awkward (lists of related objects, with methods to access them...) and hard to read.
I'm trying to find a workaround for these problems that permits a nice interface, but I'm also trying to have a first working version to release (even if it is buggy and not pretty).
Wednesday, 24 September 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment