Thursday, November 3, 2011

Java EE 5–No Entity Beans!

I’ve always done ORM with HIbernate –and it helped a lot with cardinality (besides ORM general features).

1:1::Married Couple (there are cultural and religious exceptions)

1:n:: Order: Line Item

n:1:: Line Item: Order

n:n:: Course: Student

Regardless of cardinality, entities should never be non-singular from a naming convention stand point.

In Bidirectional relationships – the inverse side must refer to its owning side via the mappedBy element.

In n:1, the many side always owns the relationship. In n:n – it doesn’t matter.

All in all the beauty of Java Persistence API is that it has all the features that Hibernate had – and then makes things a lot simpler with Annotations. I never was a fan of annotations when they first surfaced, but I have slowly come to like it. I like the encapsulation it provides.

You can mix non-entity super classes with entity super classes, use polymorphic associations, queries etc. It is surprisingly flexible.

Three basic strategies remained consistent in ORM land – 1 table per class (or class per table) – usually you will have a database structure before you have the object structure.