LucDaher
LucDaher

Reputation: 157

Hibernate Annotation - 1 to 1 with no auto_increment

How do I proceed in terms of Hibernate Annotation (JPA) when a table's primary key column is a foreign key to another table with no auto_increment (MySQL).

Thanks.

Upvotes: 0

Views: 111

Answers (1)

nick.stuart
nick.stuart

Reputation: 544

Something like the following should work (not tested for exact syntax, but should be close):

@Id
private int id;

@OneToOne
@JoinColumn(name = "id", updatable = false, insertable = false)
private RelationEntity other;

You would need to manually set your 'id' field before you persist it, and can't remember if you can set the 'other' entity before the initial save, hibernate may complain if you do that. However, if your 'id' column is set then when you load the entity back out you should have the relationship loaded as well.

Upvotes: 1

Related Questions