'Persisting unlinked object throws: During synchronization a new object was found through a relationship that was not marked cascade PERSIST

I am trying to persist an object using JPA but I keep getting this error: During synchronization a new object was found through a relationship that was not marked cascade PERSIST. The weird thing is, if I press the button to persist the object, the first time it gives this error, and after that it works perfectly.

EDIT I Have found that even removing the em.persist(doelstelling); line does not fix the issuae, so I would guess there is still something waiting to be commited?

(Classes are simplified)

Categorie.java:

@Entity
@Table(name="categories")
public class Categorie implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    
    @Column(unique=true, nullable = false)
    private String naam;
    
    @Lob
    private byte[] icoon;


// This was not commented first, but commenting did not really help and the problem persists
//  @ManyToMany
//  private Set<MvoDoelstelling> doelstellingen;
    private Set<Rol> rollen;
    
    @OneToMany(
            mappedBy = "categorie",
            cascade = CascadeType.ALL
        )
    private Set<Sdg> sdgs;

}

Doelstelling.java

@Entity
public class MvoDoelstelling extends MvoDoelstellingComponent {
    
    private static final long serialVersionUID = 1L;
    
    @Transient
    private PropertyChangeSupport support;
    
// Commented this aswell to try and eliminate all lists but no succes
//  @OneToMany(cascade = CascadeType.ALL)
//  private List<MvoDoelstellingComponent> mvoDoelstellingComponenten;
    
    @OneToOne(cascade = CascadeType.ALL)
    private Sdg sdg;
    
    
    @Column(nullable = true)
    private double drempelWaarde;
    
    @Column(nullable = false)
    private Set<Rol> rollen;
    
    @Lob
    private byte[] icoon;
}

Code to save:

    @Override
    public void saveDoelstelling(MvoDoelstelling doelstelling) {
        em.getTransaction().begin();
        em.persist(doelstelling);
        em.getTransaction().commit();
    }

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
  <persistence-unit name="xxx" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>domein.Gebruiker</class>
    <class>domein.AanmeldPoging</class>
    <class>domein.Categorie</class>
    <class>domein.Sdg</class>
    <class>domein.MvoDoelstelling</class>
    <class>domein.MvoDoelstellingComponent</class>
    <class>domein.Datasource</class>
    <properties>
      <property name="eclipselink.logging.level" value="FINE"/>
      <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
    </properties>
  </persistence-unit>
</persistence>

I do think it might have something to do with the Sdg that gets saved in both classes and maybe has a reference like that?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source