Hi!
I'm using the newest Hibernate release in a standalone application.
All of my entities have a businessKey. Entities in the database are identical, if two businessKeys are identical. This means that if an entity with the businessKey "xyz" exists and an entity with the same businessKey should be persisted, the already persistent entity should be updated.
I wrote a method which implements this behaviour:
<pseudocode>
Code:
private void saveOrUpdate(Session session, BaseEntity baseEntity) {
BaseEntity tmp = getEntityForBusinessId(baseEntity.getClass(),baseEntity.getBusinessId());
if (tmp != null) {
session.evict(tmp);
//Adopt id and version which are set by hibernate
//Other fields like (for example) username are not overwritten
baseEntity.initDatabaseFieldsFrom(tmp);
}
session.saveOrUpdate(baseEntity);
}
</pseudocode>
Every entity is persisted with the method above and it works fine. But unfortunately, due to this method, I can't use cascading, because cascading would bypass this method.
Is there an elegant way how I could implement the behaviour so that I will be able to use cascading?
Thanks for help
hoeft