Hello there,
I'm trying to update two object related between eachother by a OneToOne relation. Everytime I try to update these object a new "ownee" record is created in the RDBMS instead of updating the existing one. The error seems to be related by the the ownee's id, which on the select results to be equals to 0 (other ownee's properties are ok and visible).
I'd appreciate any help from you.
Belowe some details, including some code.
Thanks in advance.
Hibernate version: 3.2.3
Mapping documents:
Code between sessionFactory.openSession() and session.close():
Code:
public Usr getUsrById(long id) {
Usr user = null;
user = em.find(Usr.class,new Long(id));
return user;
}
...
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public boolean update(Usr user) throws Exception {
if(this.check(user)){
em.merge(user);
return true;
}else return false;
}
Name and version of the database you are using: MySql 5The generated SQL (show_sql=true):Quote:
14:43:52,196 INFO [STDOUT] Hibernate:
select
usr0_.id as id14_2_,
usr0_.password as password14_2_,
usr0_.username as username14_2_,
usr0_.regDate as regDate14_2_,
usr0_.email as email14_2_,
usr0_.verificationLink as verifica7_14_2_,
usr0_.active as active14_2_,
usr0_.skills as skills14_2_,
usr0_.minEuroSalary as minEuro10_14_2_,
usr0_.minSterlSalary as minSter11_14_2_,
usr0_.idealJob as idealJob14_2_,
usr0_.cvUriPath as cvUriPath14_2_,
usr0_.availPeriod as availPe14_14_2_,
usr0_.user_type as user1_14_2_,
addresslis1_.user_fk_id as user17_4_,
addresslis1_.id as id4_,
addresslis1_.id as id15_0_,
addresslis1_.address as address15_0_,
addresslis1_.companyName as companyN3_15_0_,
addresslis1_.title as title15_0_,
addresslis1_.firstName as firstName15_0_,
addresslis1_.lastName as lastName15_0_,
addresslis1_.city as city15_0_,
addresslis1_.postalCode as postalCode15_0_,
addresslis1_.stateOrProvince as stateOrP9_15_0_,
addresslis1_.countryOrRegion as country10_15_0_,
addresslis1_.phoneNumber as phoneNu11_15_0_,
addresslis1_.faxNumber as faxNumber15_0_,
addresslis1_.mobileNumber as mobileN13_15_0_,
addresslis1_.notes as notes15_0_,
addresslis1_.vat as vat15_0_,
addresslis1_.website as website15_0_,
addresslis1_.user_fk_id as user17_15_0_,
usrrole2_.id as id16_1_,
usrrole2_.username as username16_1_,
usrrole2_.userRole as userRole16_1_
from
USR usr0_
left outer join
USR_ADDRESS addresslis1_
on usr0_.id=addresslis1_.user_fk_id
left outer join
USR_ROLE usrrole2_
on usr0_.id=usrrole2_.id
where
usr0_.id=?
order by
14:43:52,196 INFO [STDOUT] Hibernate:
select
usr0_.id as id14_2_,
usr0_.password as password14_2_,
usr0_.username as username14_2_,
usr0_.regDate as regDate14_2_,
usr0_.email as email14_2_,
usr0_.verificationLink as verifica7_14_2_,
usr0_.active as active14_2_,
usr0_.skills as skills14_2_,
usr0_.minEuroSalary as minEuro10_14_2_,
usr0_.minSterlSalary as minSter11_14_2_,
usr0_.idealJob as idealJob14_2_,
usr0_.cvUriPath as cvUriPath14_2_,
usr0_.availPeriod as availPe14_14_2_,
usr0_.user_type as user1_14_2_,
addresslis1_.user_fk_id as user17_4_,
addresslis1_.id as id4_,
addresslis1_.id as id15_0_,
addresslis1_.address as address15_0_,
addresslis1_.companyName as companyN3_15_0_,
addresslis1_.title as title15_0_,
addresslis1_.firstName as firstName15_0_,
addresslis1_.lastName as lastName15_0_,
addresslis1_.city as city15_0_,
addresslis1_.postalCode as postalCode15_0_,
addresslis1_.stateOrProvince as stateOrP9_15_0_,
addresslis1_.countryOrRegion as country10_15_0_,
addresslis1_.phoneNumber as phoneNu11_15_0_,
addresslis1_.faxNumber as faxNumber15_0_,
addresslis1_.mobileNumber as mobileN13_15_0_,
addresslis1_.notes as notes15_0_,
addresslis1_.vat as vat15_0_,
addresslis1_.website as website15_0_,
addresslis1_.user_fk_id as user17_15_0_,
usrrole2_.id as id16_1_,
usrrole2_.username as username16_1_,
usrrole2_.userRole as userRole16_1_
from
USR usr0_
left outer join
USR_ADDRESS addresslis1_
on usr0_.id=addresslis1_.user_fk_id
left outer join
USR_ROLE usrrole2_
on usr0_.id=usrrole2_.id
where
usr0_.id=?
order by
addresslis1_.address asc
addresslis1_.address asc
"Owner" class
Code:
public class Usr implements java.io.Serializable {
...
@Id
@GeneratedValue
@Column()
public long getId() {
return id;
}
...
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
public UsrRole getUserRole() {
return userRole;
}
Ownee class
Code:
public class UsrRole implements java.io.Serializable {
...
@Id
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}