you dont want to craete a new instance (it will always insert), but update an existing one. well, if that's the case i think there is something wrong with your model.
if you have many-to-one with unique constraint, it is in fact one-to-one, so each of your processDef is allowed to have exactly one processStatus instance, am i correct?
if so, you can easilly pick the one you want -- there is only one that matches (you may use hql for example and join ProcessStatus and ProcessDefinition). if you really try to model 1:1 with foreing-key and unique (nothing wrong with that), consider making the association bidirectional
the parent part:
Code:
<one-to-one name="operation" class="BookEntryOperation" cascade="all"
property-ref="bookEntry"/>
the child part:
Code:
<!-- should be uniq, 1:1, some db (MSSQL) have problems -->
<many-to-one name="bookEntry" class="BookEntry" column="book_entry_id"
not-null="false" unique="false"/>
just note that i have unique=false in child part, you will have unique true. the problem with my mapping is that is use inheritance and book_entry_id column must be enabled to store NULL values (table-per-hierarchy strategy), in case of MSSQL server for example there are forbuidden multiple NULL values in one unique column (at least i do not know how to enable :-)
once you have it bidirectional, no problem to write the code (and a proper update will be issued -- check it) -- you will be able from processDefinition with single property getter load the proper processStatus.
you can do for example:
Code:
Session session = sessionFactory.getCurrentSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.lock(processDefinition);
// i assume there is always instance of processStatus attached to processDefinition
processDefinition.getProcessStatus().setActive("active");
session.saveOrUpdate(processDefinition);
tx.commit();
} catch (HibernateException e) {
if (tx != null) tx.rollback();
e.printStackTrace();
session.close(); // if you keep the sesion opened for current thread untill error
}
was this helpfull?
just an note: i would guess that from a ProcessDefinition there can be several running processes of that type (1:n) independently and each of them should have their state (1:1) (ProcessSatus) -- in that case i miss the 1:n relation.
m.