When trying to save an ID from my parent class into a child class, I keep getting the error
“ERROR – Field ‘parent_id’ doesn’t have a default value”
I have tried all types of mappings. I am using annotations.
Any help on this would be appreciated
Parent:
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@Column(name="description")
private String description;
@OneToMany
@Cascade(value= {org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE})
@JoinColumn(name="parent_id")
private List<Child> children;
Child:
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@Column(name="description")
private String description;
Thanks.
You must have something wrong somewhere else because those mappings will work the way they are. They could be better, but they’ll work. Specifically, all the
@Columnannotations are redundant and unnecessary, and as non sequitor noted, you should use the cascade property of JPA’s@OneToManyinstead of Hibernate’s@Cascade. I’ve created a runnable example with the cleaned-up version of what you posted. If you have git and maven, you can run it with:It creates a parent with two children, saves them, and then loads them and prints out the graph. The output is: