Monday, May 25, 2009

Model View Controller (MVC) Pattern

The Model View Controller (MVC) pattern implements the Separation of Concerns Principle at application level. Back-end and front-end logic are separated with a controller layer that glues them together while keeping them separated at the same time. This is why complex application are more manageable and maintainable when implementing this pattern. The separation between the two makes it easy to change the implementation of one, without changing the implementation of the other. The controller is the one that will suffer changes in order to the new implementation.


The model represents the data and the business rules of your application. It usually referred to as the back-end and consists of two layers: the persistence layer, and the business layer. The persistence layer is responsible for retrieving and storing data in a database while the business layer implements the business rules that manage the data. The model may notify the view about changes in its state.

The view is the layer that implements the interface with the user. The interface displays the data of the model in a human understandable manner. The view may query the state of the model or respond to model state change notifications to keep itself synchronized with the model.

The controller translates user actions into actions that activate business processes in the model. Running this business processes usually produce changes in the state of the model. The controller chooses which view to display to the user as a result of a user action. When the chosen view will be rendered, the current state of the model will be displayed to the user.

Wednesday, May 6, 2009

JPA does not delete orphans

Don't really understand way, but there seems to be a limitation in the framework that does not permit this. If you search the net you'll find many frustrated programmers because of this.

There are some solutions. These solutions imply that the foreign key in the child table allows null. Without this, we can not obtain orphans (a row in the child table which has the foreign key to the parent set on null), and you will get an exception before having the "not deleted orphans" problem. If you need that constraint: foreign key in child must not allow null, there may be another solution which I hope to make time and test so I can post it here soon.

1. If you use a Hibernate implementation behind JPA, you can use the Hibernate annotation @Cascade(CascadeType.DELETE_ORPHAN) .

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

@OneToMany
@Cascade(CascadeType.DELETE_ORPHAN)
private List childList;

<dependency>
<groupid>org.hibernate</groupid>
<artifactid>hibernate-annotations</artifactid>
<version>3.4.0.GA</version>
</dependency>

2. Manually delete the orpnas after each update that denerate orphans.

entityManager.merge(parentEntity);
em.flush();

//delete orphans
em.createQuery(
"DELETE ChildEntity child WHERE child.parentEntity IS NULL").executeUpdate();

Well, that's life, you got to be a killer from time to time... poor orphans

If you have questions, just ask.