My Content has a Map of Footnote, and Footnote has a content_id column which is a foreign key back to Content. Unfortunately saving my Content with the footnoteMap containing a Footnote throws the following error:
ERROR: Cannot insert the value NULL into column 'content_id', table 'CMT_DEV.dbo.footnote'; column does not allow nulls. INSERT fails.
SEVERE: org.springframework.dao.DataIntegrityViolationException: Cannot insert the value NULL into column 'content_id', table 'CMT_DEV.dbo.footnote'; column does not allow nulls. INSERT fails.; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Cannot insert the value NULL into column 'content_id', table 'CMT_DEV.dbo.footnote'; column does not allow nulls. INSERT fails.
I must be missing something with the annotations required to make this work, but I can’t figure out what it is.
Here is my JPA mapping:
public class Content implements EntityModel, Serializable {
@OneToMany(mappedBy="contentId", cascade=CascadeType.ALL)
@MapKey(name="number")
@OrderBy("number ASC")
private Map<Integer, Footnote> footnoteMap;
....
}
public class Footnote implements EntityModel, Serializable {
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "content_id", referencedColumnName= "id")
private Content contentId;
....
}
* update *
Keep in mind that the point at which a Footnote is added, the content item has not been saved.
First of all, note that it’s your responsibility to keep both sides of bidirectional relationships in consitent state, i.e. when you add a
FootnotetoContentyou should also set itscontentIdappropriately.If you did so but it still fails, then perhaps it’s a mismatch between database schema and Hibernate mapping. Note that
content_idcolumn has a not null constraint, but nothing in the mapping tells Hibernate that such a constraint exists. Try@ManyToOne(cascade=CascadeType.ALL, optional = false).