one-to-one with manually assigned identifiers
I have a troubles with a one-to-one mapping.
I read the half day through the forums, but couldn't find an appropriate answer.
using hibernate 3.1 beta 2 with annotations 3.1 beta 4
I manually assign guids as my id.
I have an unidirectional one-to-one mapping from class A to class B.
The generated schema includes an foreign key constraint in table A to the primary key in table B.
I configured versioning with int as type. During object instanciation the guid as id is generated by the constructor and zero value ist assigned to version.
In the one-to-one mapping I have defined Cascade.ALL
when i try the following a foreign key violation occurs:
B b = new B();
A a = new A();
a.setB(b);
saverOrUpdate(A);
From what I read from the reference guide about automatic state detection, I would not expect any trouble. Hibernate should recognize, that A AND B are transient and insert it.
Here is the code:
Code:
@Entity
@Table
public class A {
protected String id;
protected int version;
protected B b;
public A() {
super();
id = java.util.UUID.randomUUID().toString();
version = 0;
}
/**
* I also tried CascadeType.ALL
*/
@OneToOne(targetEntity=B.class,cascade = {CascadeType.PERSIST,CascadeType.MERGE})
@JoinColumn(name="b_fk")
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
@Id
@Column(name="id", length=36)
public String getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(String id) {
this.id = id;
}
@Version
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public final boolean equals(Object other) {
if(other == this)return true;
if (other==null ||!(other instanceof A)) return false;
A castOther = (A) other;
return id.equals(castOther.getId());
}
@Override
public final int hashCode() {
return getId().hashCode();
}
}
@Entity
@Table
public class B {
protected String id;
protected int version;
public B() {
super();
id = java.util.UUID.randomUUID().toString();
version = 0;
}
@Id
@Column(name="id", length=36)
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Version
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public final boolean equals(Object other) {
if(other == this)return true;
if (other==null || !(other instanceof B) ) return false;
B castOther = (B) other;
return id.equals(castOther.getId());
}
public final int hashCode() {
return getId().hashCode();
}
}
Any ideas what I am doing wrong?
Thx
Nils