How to find out if Hibernate has been inserted or updated AFTER running getHibernateTemplate().saveOrUpdate(object); , the method is invalid!
saveOrUpdate () does the following:
- If the object is already persistent in this session, do nothing.
- if another object associated with the session has the same identifier, throw an exception
- if the object does not have an identifier property, save () it
- if the object identifier has the value assigned to the newly created object, save () it
- if the object has a version using a or, and the value of the version property is the same value assigned to the newly created object, save () it
- otherwise update () the object
I'm afraid that I always need to check if the object already has a "primary key / identifier" before running this method - is this the only way? If so, how can I get the primary key / identifier in a general way?
Serializable id = session.getIdentifier(entity); or Object id = entityManagerFactory.getPersistenceUnitUtil().getIdentifier(entity); ??
source share