Hi. I apologize if that's a stupid question, but I have an application with entity classes that contain a synthetic key for hibernate (a Long), and a natural keys (which is used in the equals() implementation). I have have a scenario (which I guess is not uncommon), where I have a set of objects, for which I know the natural keys values, but I don't know if there is a matching row in the database. I would like to save the entries that don't have one, and update the ones that do. I guess Session.saveOrUpdate() would not be useful in this situation, since I don't know the synthetic key values of the object (if it's saved), only the natural one. I could use for any of those objects an hql query that tries to retrieve it by it's natural keys values, but this feels a little inefficient and overly complex, and I can just try to save() it and handle an exception from JDBC, if the matching row is already in the database (assuming I assign a unique key to the class's natural keys) is there a better way that I am missing here ? Thanks.
|