Dear all,
I've a great issue with hibernate, with annotated classes.
I've a 1 - relationship (i've this issue also with 1-n relationship) between user and role, that is a user must have one and one only role.
In my User class I've
Code:
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="ID_ROLE)
private Role cRole;
Clicking on Save button I check if it's a new user or existing one.
// save if new, if not update
Code:
if(this.m_cObject==null)
{
cUserDao.create(cUser);
} else {
long id=((User)m_cObject).getIdUser();
cUser.setIdUtente(id);
cUserDao.update(cUser);
}
cUserDao.update(cUser) calls this method
Code:
protected void saveOrUpdate(Object obj) {
try {
startOperation();
session.saveOrUpdate(obj);
tx.commit();
} catch (HibernateException e) {
System.out.println("Errore nel salvataggio oggetto : "+e.getMessage());
handleException(e);
} finally {
HibernateFactory.close(session);
}
}
with
Code:
protected void startOperation() throws HibernateException {
session = HibernateFactory.openSession();
tx = session.beginTransaction();
}
Result is this one:
When I edit user and I change role and save it, user is correctly update and also role si updated, but a new row for role is added.
I noted the same one on 1-n relationship, father of relationship is correctly updated, but for child ones a new row is added.
Why it?
Could you help me?
Please, thanks