I have the following 2 classes:
@Entity
@Table(name = "TableA")
public class EntityA
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Id")
private final Integer id = null;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "BId")
private EntityB b;
public EntityA(EntityB b)
{
this.b = b;
}
}
@Entity
@Table(name = "TableB")
public class EntityB
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Id")
private final Integer id = null;
@OneToOne(mappedBy = "b")
private final EntityA a = null;
}
When I do
session.save(new EntityA(new EntityB());
the database only inserts a record in TableA and leaves the column that references TableB as NULL
If I first insert b, then a, it works, but it should work with a single call too.
Other questions/answers mention that the annotations are not correct, but I see no difference between mine and the provided solutions.
I also tried adding the CascadeType.PERSIST on both @OneToOne annotations, but that didnt work either.