a new object was found through a relationship that was not marked cascade PERSIST

What you had likely done is that you created new instance of Article and and some new instance(s) of HeaderField. These instance(s) of HeaderField were then associated with Article.

After that trying to persist Article fails, because as error message says, it refers to new objects and relationship is not marked as PERSIST. Additionally according your logs these instances of HeaderField does not have headerName and headerValue set.

You have two options:

  1. persist also other instances referenced from Article via em.persist
  2. cascade persist operation from Article to HeaderFields with following

    OneToMany(mappedBy = "article", cascade = CascadeType.PERSIST)  
    private List<HeaderField> someOrAllHeaderFields = new ArrayList<>();
    

Additionally you should not remove no-arg constructor. JPA implementation always calls this constructor when it creates instance.

But you can make no-arg constructor protected. In JPA 2.0 specification this is told wit following words:

The entity class must have a no-arg constructor. The entity class may
have other constructors as well. The no-arg constructor must be public
or protected.

Leave a Comment