I think this does what you want.
Parent:
Code:
@Entity
public class Parent {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@OneToOne(mappedBy = "parent", fetch=FetchType.LAZY, cascade=CascadeType.ALL, optional=true)
private Child child;
}
Child with the same ID as the parent:
Code:
@Entity
public class Child {
@Id @GeneratedValue(generator="parentPKGenerator")
@org.hibernate.annotations.GenericGenerator(
name = "parentPKGenerator",
strategy ="foreign",
parameters = @org.hibernate.annotations.Parameter(name = "property", value = "parent")
)
private int id;
@OneToOne(optional=false, fetch=FetchType.LAZY)
@PrimaryKeyJoinColumn
private Parent parent;
}
Generates the following for mysql:
Code:
create table Child (
id integer not null,
primary key (id)
) type=InnoDB;
create table Parent (
id integer not null auto_increment,
primary key (id)
) type=InnoDB;
alter table Child
add index FK3E104FC5F807672 (id),
add constraint FK3E104FC5F807672
foreign key (id)
references Parent (id);