Here is the relevant part of my JPA-annotated class (it's an item belonging to a category and also having an owner; both of these are many-to-one relationships):
Code:
{
public class Item {
@Id
@GeneratedValue
private long id;
@NotNull
private String name;
@ManyToOne()
@JoinColumn(name="item_category_id")
@NotNull
private ItemCategory itemCategory;
@ManyToOne()
@JoinColumn(name="owner_id")
@NotNull
private Person owner;
...
}
If I use hbm2ddl, I find that a strange extra foreign-key constraint is produced (HSQL):
Code:
alter table Item add index FKACE9553AC59115F (owner_id), add constraint FKACE9553AC59115F foreign key (owner_id) references ItemCategory (id);
alter table Item add index FKACE9553A77E487B4 (owner_id), add constraint FKACE9553A77E487B4 foreign key (owner_id) references Person (id);
alter table Item add index FKACE9553AC16B9B52 (item_category_id), add constraint FKACE9553AC16B9B52 foreign key (item_category_id) references ItemCategory (id);
Woah! Why two foreign keys for owner_id, one referencing table ItemCategory!?
My initial thought was that this is a Hibernate
ddl generation defect, so I simply dropped the bad foreign key.
But one of my unit tests were failing to fetch join the right Item for the ItemCategory class, and I believe this is because Hibernate's schema/metadata is confused on how to join the table.
Or perhaps my annotations are incorrect.
Can someone comment? Also to note, I'm using Hibernate 3.2.5.ga.