I'm using SPRING DATA JPA and I have following entities:
public class EntityA {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SOME_SEQ")
@SequenceGenerator(sequenceName = "SOME_SEQ", allocationSize = 1, name = "SOME_SEQ")
private Long id;
private String name;
@OneToOne(mappedBy = "entityBField", cascade = CascadeType.ALL)
private EntityB entityB;
}
public class EntityB {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SOME_OTHER_SEQ")
@SequenceGenerator(sequenceName = "SOME_OTHER_SEQ", allocationSize = 1, name = "SOME_OTHER_SEQ")
private Long id;
private Integer type;
@OneToOne
@JoinColumn(name = "entityaId", referencedColumnName = "id")
private EntityA entityBField;
}
This is the tables definition:
create table EntityA(
id int primary key,
name varchar(255)
);
create table EntityB(
id int primary key,
type int,
entitya_Id int FOREIGN KEY ("entitya_Id")
REFERENCES "EntityA" ("id")
);
I create and save one instance as follow:
EntityA a = new EntityA();
EntityB b = new EntityB();
a.setEntityB(b);
entityARepository.save(a)
What happens is that both entityA and entityB are persisted, but the foreign key entityaId on EntityB is null. Could you suggest how should I persist the foreign key linking EntityB to EntityA?