PDA

View Full Version : Transparent persistence tool and DAO pattern



scesbron
Dec 21st, 2004, 05:25 AM
Hi,

I have some troubles using transparent persistence tool with the DAO pattern. In my case I am using hibernate

1/ With automatic dirty checking, hibernate flushes modifications on my domain object each time I do a query. How can I handle this if I do some queries to validate my object ?

2 / Unique constraint

This is my main problem and the most generic one. Transparent persistence tool like hibernate and jdo give me automatic dirty checking. So, as I saw in juergen's chapter on dao, I do not need to call explicit update methods. So how can I verify my unique constraint ?

I saw on different threads that I can use my db constraints' names and rely on the db to verify my constraints. I have a problem with that : how can I achieve that when I persist a class hierarchy in one table ? I can have a unique constraint on one part of the hierarchy (one child class) but not on the whole hierarchy. Thus I cannot put my constraint in my db.

Here are my thought on how I can handle the problem

a) I put my check in an update method inside my dao. With this solution I have two possibilities

Developpers have to call this method but I cannot ensure they do
I add some home made code in my app to partially disable automatic dirty checkin. I throw an exception if hibernate try to update an object without previous call to my method : this seems a little bit tricky for dependent objects

b)I use the validator to ensure my unique constraint : I do not know if this is possible. To do that my validator must have access to the hibernate session.

With both solutions I fall into my first problem. to test uniqueness on update I have to count the number of elements in the database but if I do so hibernate flush my session and update my object in the db before the count.

Another problem with transparent persistence framework. I would like to have some 'onSave' and 'onUpdate' events thrown each time a domain object is created or modified. I would like to handle this in my daos but how can I achieve this without update method in my daos.

In conclusion I would like to know why I seem to be the only one disturb with automatic dirty checking.

thanx in advance

Seb

tentacle
Jan 10th, 2005, 10:54 AM
1/ mark the method on your dao that does the query as "PROPAGATION_REQUIRES_NEW,readOnly". This will make sure the Hibernate session in not flushed. Make sure you use the proxy created by TransactionProxyFactory bean and not the dao itself.

2/ It's good design to enforce constrainst in the database where possible, not in the application. The one-table-per-hierarchy approach offered by Hibernate can introduce a number of nullable fields in your table which cannot be checked by a unique constraint. The only really good solution for this are triggers. A trigger could check per value of the discriminator field if some constraint (required, unique) is not violated.

Inheritance in Hibernate is not an exact science. Triggers are probably not a good solution if you do a lot of inserts or updates because they are quite heavy to execute. When nullable fields are a problem you may consider to use the joined-table approach also offered by Hibernate. This ofcourse is not a good solution if you are doing a lot of queries on that table or if your table contains really a lot of data that needs to be queried.

If database performance while inserting or selecting is an issue don't use inheritance, that's my advise.