Hi,
I'm using hibernate 3.3.1.GA with annotation 3.4.0.GA to access a local mysql database. Currently I also use hibernate to create/update my database during program startup by setting the "hibernate.hbm2ddl.auto" property to "update". But unfortunately I have some objects which use only the Integer or Long primary key to reference an associated object - e.g.
Code:
class A {
@Id
private int id;
}
class B {
@Id
private int id;
@Column(name = "a_id");
private int myAId;
}
Unfortunately I'm unable to create forein key constraints in the database this way since a possible @ForeignKey annotation is ignored.
However I'm able to let hibernate create the foreign key constraint if I change the annotations like follows:
Code:
class B {
@Id
private int id;
@ManyToOne(optional=true, targetEntity=A.class)
@ForeignKey(name="FK_B_A_ID")
@JoinColumn(name = "a_id")
private int myAId;
}
However this causes write and/or read accesses to the B table to fail with a "java.lang.IllegalArgumentException: object is not an instance of declaring class". Hibernate seems to try to the set id property of the A class on a primitive of the type int.
So I wonder if there is any way to cause hibernate to create foreign key constraints without having to use full Object references in the object model. (To have B reference a lazy loaded instance of A is not an option because the object is serialized and sent to other machines).