Friday, 17 October 2014

differences between getPostState() and getEntityState()

If you don't pay a lot attention to the subject of this post it'll sound the same. First of all what is the role of an entity. It is something like a mirror image of database table which takes a part during object-relational mapping process. It could contain transient attributes which aren't persisted. It can be based on other database objects than a table. But more or less we can understand an entity as database table definition in ADF.

ORM role implies on it states. I think that everyone understand that the state machine implemented there will be quite complex and the transitions will depends on operations performed on the entity. It is illustrated on the graph below:

For both of the cases the graph look similar but the meaning of the states is a bit different. Entity state represents corellation between object attributes value and the database. Post state is related to database transaction itself.

Lets deeper look to entity state first. There is no default initial state for the entity. There are two states that can be assumed as defaults depends on origin. STATUS_NEW, with byte value equal 0, while current entity is created in the middleware and will be commited within the database transaction. STATUS_UNMODIFIED, byte value 1, if the current changes entity has been commited or it has been synchronized with database (read from the database).

After at least one attribute is set, entity goes to STATUS_MODIFIED (2). STATUS_DELETED with 3 byte value is when a row will be deleted. The last option is STATUS_DEAD (4). Entity has been created and after that removed within the same db transaction or it has been deleted and DELETE DML operation has been performed on the database.

Post state holds information for the transaction manager what have already been done with the data within database transaction. Post state is new when a row is created in the middleware and waits to be commited. If you delete such row it'll go to STATUS_DEAD. STATUS_MODIFIED and STATUS_DELETED are also markers for related DML operations. Interesting is the STATUS_INITIALIZED with -1 value. It excludes the row from the database transaction, so it marks it as temporary one.

These two states are tightly coupled but usually you won't need to worry about it. Framework itself will carry on the transitions. The problem that i've struggled with was while doDML entity method thrown an exception. Framework thought that the entity has been commited (getPostState()==STATUS_UNMODIFIED) but getEntityState() was still STATUS_NEW.

No comments:

Post a Comment