Patterns & OO Design - Simple ORM question

I have a problem with Object Relational Mapping which I can't find anywhere.

From visiting dozens of sites I finally found some usefull info to model the database to code in ORM style.

But now my head is against a wall again after modeling a big part of the ORM.

What im having now is something like this:

A relational database is set up, in this example a machine is the main table that has a foreign key to a bus (that is on the machine).

This bus has an ecu through a foreign key, etc. etc.

Sorta like a tree structure.

Now I want to use the object relational thinking to program my code, with as far as i know, having each table as classes.

But now if i want to get the submit date for a parameter on some machine for example, i have to type this:

submitDate = machine.getBus().getEcu().getSoftware().getParameterValue().getParameterDesciption().getSubmitDate();

Is this normal?

I can't think or find anything better on how to model it, hope someone could help me :) would really appreciate it.

[1073 byte] By [radicjesa] at [2007-11-26 23:22:20]
# 1

I can't speak to the 'purity' of ORM.

But in normal non-trivial enterprise systems it is often the case that one must use pass through SQL. This can be driven either by performance or/and complexity issues which would be incurred by only using the data layer approach.

For pass through SQL one crafts a custom SQL statement which returns everything that a business driven requirement needs. And that is used to get the data. Sometimes that drives directly to existing data model objects but sometimes it is better to create a custom object for that as well.

jschella at 2007-7-10 14:27:21 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

From a very EJB-like bird's eye, ORM software might be good for getting objects into a relational format. Building an application with an ORM technology might help reduce and/or eliminate hand-written SQL statements and JDBC|JNDI code. This might help to speed the development cycle because of the reduced testing and coding. This is a benefit.

That being said, getting relational data into object form is a different process that an ORM tool can indeed do, but why do it? As mentioned, using a custom SQL statement is a much cleaner| easier| friendlier method of getting relational data for display.

If an application needs to instantiate an object form of the relational data then using an ORM makes good sense. If an application simply needs to display the relational data, then using an ORM is too much and should be avoided, in my opinion.

In any case, the relationships for ORM should not exceed 3, in my opinion.

GhostRadioTwoa at 2007-7-10 14:27:21 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

Thanks for the answers.

As to "In any case, the relationships for ORM should not exceed 3, in my opinion." i found a solution for that and my basic problem.

If i ever need something on my website that i need for example the date of that parameter value, then I could just make a shortcut getter in the machine instead of going down the entire tree writing a long line.

And as far as i know this does not break any model.

So now I would type something like

machine.getParameterDescription().getDate()

instead of first getting the bus, ecu, software and parametervalue everytime i want that

radicjesa at 2007-7-10 14:27:21 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

In such cases, shortcut methods are indeed useful. If the DTO's are generated (Hibernate?) you might find it useful too to just extend the original DTO and add the shortcut getters to it instead of changing the orignal DTO.

public class Machine {

private Bus bus; // + getter + setter

...

// Keep it original.

}

public class MyMachine extends Machine {

public Date getDate() {

return getBus().getBlah() ... getDate();

}

}

BalusCa at 2007-7-10 14:27:21 > top of Java-index,Other Topics,Patterns & OO Design...
# 5

In such cases, shortcut methods are indeed useful. If

the DTO's are generated (Hibernate?) you might find

it useful too to just extend the original DTO and add

the shortcut getters to it instead of changing the

orignal DTO.

Why not just provide a helper class/method and thus avoid any confusion about why the inheritence was used?

jschella at 2007-7-10 14:27:21 > top of Java-index,Other Topics,Patterns & OO Design...
# 6

ORM is the code that maps the database to the object world. But I don't think its very good to use ORM directly throughout yoru code. Once you get the data mapped, you need to them encapsulate it in a more OO fashion for use throughout the rest of your program.

One class per table is not a good way to think about it. When you are using data you should not even be thinking about tables. You probably need to add a layer of abstraction on top of your ORM before you use it in the rest of your program.

_dnoyeBa at 2007-7-10 14:27:21 > top of Java-index,Other Topics,Patterns & OO Design...