The whole idea of Hibernate is that you don't need to know SQL or JDBC anymore to persist your database model.
But it is not so easy as it seems.
You can get this exception when saving:
org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session:
Or this exception:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:
It turns out that Hibernate can detect when an object has been loaded twice, but it signals the error too late, when you try to save it.
The documentation on this subject is sparse. Sometimes it mentions that you haven't coded appropiate equals and hashCode methods and you have inserted this objects in Sets or Maps that span more than one Hibernate session. So when another session loads the object it can't know the object already exist, but it knows when it saves the objects? Come one!
Why doesn't it simply do the same check when loading the objects (possibly replacing the copy with the original object) is beyond my comprenhesion.
In other postings it is mentioned that Hibernate needs equals and hashCode even for object not stored in other places, since you can have proxies for objects, and the proxies could not detect that the objects were already loaded. Can Hibernate make it more complex?
Complete Abstractions
I think the real problem is that the Hibernate model is not a complete abstraction.
What is an uncomplete abstraction?
It is an abstraction that doesn't handle border cases appropiately. Joel calls it leaky abstractions, but not all abstractions are leaky. The ones which are leaking can always be backed up with new abstractions.
For example, in the case of Hibernate, the object model is persisted automagically, but I have to say when. with saveOrUpdate. Also I can't say save this but not that, all I can do is to configure it in a dumb xml file. It is dumb because it is necessarily hardcoded.
Xml is not for configuration, it was created for applications to talk, not humans to read or write. Developers who relay on xml too much are typically substandard developers.
No hay comentarios:
Publicar un comentario