First of all, I know this should not be a solution, but I need to fix the problem this way.
I have a OneToOne relation that, very rarely, is not respected (DB returns two rows instead of one). I need to force this relation, something like a LIMIT 1 for this specific Hibernate query.
@Entity
@Table(name="contact", uniqueConstraints = @UniqueConstraint(columnNames="user_id"))
public class ContactDTO implements Serializable {
(...)
// Force this relation
@OneToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY, optional = true)
@JoinColumn(name = "user_id", unique = true, insertable = false, updatable = false)
public UserDTO getBaseUser() {
return baseUser;
}
public void setBaseUser(UserDTO baseUser) {
this.baseUser = baseUser;
}
@Column(name = "user_id", unique = true)
public Integer getUserId() {
return this.userId;
}
@Entity
@Table(name = "base_user")
public class UserDTO implements Serializable {
(...)
@OneToOne(fetch = FetchType.LAZY, mappedBy = "baseUser")
public ContactDTO getContact() {
return contact;
}
public void setContact(ContactDTO contact) {
this.contact = contact;
}
When I get two rows (maximum), Hibernate throws the following exception:
org.hibernate.HibernateException: More than one row with the given identifier was found
Is this possible or I really need to turn this relation into a @OneToMany?
Note: Hibernate 3.3, sadly I can not use JoinColumnsOrFormula.
Thank you for your help! Best regards.