jeudi 5 février 2015

Check for existing entity with the exact same properties

I have a case where I save some nested structure - I create a db record with a foreign key to some other new record. In some cases I'd like the referenced record to be reused if it already exists in db with the exact same values (all but ID). (Note that this is meant as an optimization. Unique indexes are no applicable here and I don't want to enforce the uniquness for all records.)


I'm looking for a generic way to check if a duplicate-to-be record already exists and to return it if so.


Some already provided Hibernate feature would be nice. But I can also go with some custom utility method which would generate the query. I also thought of using reflection to get column properties from entity and to build the query for them, but I'm not sure how to get these annotated properties.


So far I satisfied with custom method/query per entity, but it's a tedious task. Also it's easy to introduce an error when someone adds a property but forgets to add it to the query.






Aucun commentaire:

Enregistrer un commentaire