Answer the question
In order to leave comments, you need to log in
Why is there an error when saving?
There is a (simplified) class diagram like this:
@Entity
@Table(name = "my_sub_model")
public class MySubModel {
@Id
@GeneratedValue
@Column
public Long id;
@Version
private Integer version;
@Basic(optional = false)
@Column
public String name;
}
@Entity
@Table(name = "my_model")
public class MyModel {
@Id
@GeneratedValue
@Column
public Long id;
@Version
private Integer version;
@Basic(optional = false)
@OneToOne
@JoinColumn(name = "sub_model_id")
public MySubModel subModel;
}
public void addMyModel(MyModel model) {
em.persist(model);
}
{
"id": null,
"subModel": {
"id": 1,
"name": null
}
}
Answer the question
In order to leave comments, you need to log in
After I did this in DAO
The problem was finally solved and everything was added!
You need to add cascading on save:
@Basic(optional = false)
@OneToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "sub_model_id")
public MySubModel subModel;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question