Friday, June 8, 2007

Domain Model

Implementing a Domain Model

Business concerns should be separated from the crosscutting concerns (such as transactions, persistence and authorization) - leakage of concerns.

Domain model implementation is such an important piece of code that it shouldn't depend on other Java APIs. For example, code in the domain model shouldn't perform JNDI lookups or call the database via the JDBC API.This alows you to reuse the domain model implementation virtualy anywhere. Most importantly, it makes it easy to unit test the domain model outside of any application server or other managed environment.

Transparent persistence means a complete separation of concerns between the persistent classes of the domain model and the persistence logic itself, where the persistent classes are unware of - and have no dependency to - the persistence mechanism.

Hibernate compares the objects by value - not by object identify - to determine if the property's persistent state needs to be updated. For example, the following getter method won't result in unnecessay SQL UPDATEs:

public String getFirstname() {
return new String (firstname);
}

However, there is one very important exception. Collections are compared by identify!
The following code should be avoided:

public void setNames(List namesList) {
names = (String[]) namesList.toArray();
}

public List getNames() {
return Arrays.asList(names);
}

It is recommended practice to use one mapping file per persistent class. The convention is to give the file the same name as the mapped class, appending an hbm suffix: for example, Category.hbm.xml.

Property and class mappings

Hibernate uses reflection to determine the Java type of the property and therefore the type name can be omitted.

hbm2ddl tool can be used to generate database schema.
not-null attribute should be used to report illegal null property values without going to the database.



Detection of illegal null values is mainly useful for providing sensible exceptions
at development time. It isn’t intended for true data validation, which is outside
the scope of Hibernate.

Derived properties

The value of a derived property is calculated at runtime by evaluation of an expression. You define the expression using the formula attribute. For example

formula="TOTAL + TAX_RATE * TOTAL"
type="big_decimal"/>

The given SQL formula is evaluated every time the entity is retrieved from the database.

Identify versus equality
Object identity, ==, is a notion defined by the Java virtual machine. Two object references are identical if they point to te same memory location.

On the other hand, object equality is a notion defined by classes that implement the equals() method, sometims also referred to as equivalencee. Equivalence means that two different (non-identical) objects have the same value.

Database identify - Objects stored in a relational database are identical if they represent the same row, or equivalently, share the same table and primary key value.

No comments: