Mysterious "Attempt to persist a reference to a non managed instance" error


I've been working with ObjectDB for the past several months, and it's behaved pretty much as expected. I made a few modifications to a working set of classes today, and it's started throwing exceptions at commit time. I've stripped it down to a  set of files that seem to be of trivial complexity, but it's still failing.

It involves two entities, Index and Ostrich. The thing to note is that the Index structure is recursive through maps; but that's never caused any problem for me in the past.

package failing;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity public class Ostrich implements Serializable {
    @Id long id;
    public Ostrich() { }
package failing;
import java.util.Map;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import static javax.persistence.CascadeType.*;
import static javax.persistence.FetchType.*;

@Entity public class Index implements Serializable {
    @Id @GeneratedValue private long id;
    @OneToOne(fetch=EAGER, cascade=PERSIST) public Ostrich ostrich;
    @ManyToMany(cascade=ALL) public Map<String, Index> entries;

    public Index(Ostrich ostrich, Map<String, Index> entries) {
      this.entries = entries;
      this.ostrich = ostrich;

   @SuppressWarnings("unused") private Index() { }

   public Ostrich getOstrich() { return ostrich; }

...and a program to try it out:

package failing;
import java.util.HashMap;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import com.objectdb.Enhancer;

public class ODBTest {

  public static void main(String[] args) {
      EntityManagerFactory emf = Persistence.createEntityManagerFactory("./db/db0.odb");
      EntityManager em = emf.createEntityManager();
      Ostrich oo = new Ostrich();
      Index dd = new Index(oo, new HashMap<String, Index>());
      HashMap<String, Index> dEntries = new HashMap<String, Index>();
      dEntries.put("xxx", dd);
      Ostrich o = new Ostrich();
      Index d = new Index(o, dEntries);
      EntityTransaction tx = em.getTransaction();

I had started with the expectation that the cascading annotations would be sufficient to cause everything to be persisted (and that's worked well for me in the past), but no cigars. So, you'll note that all four of the entities (2 Index, 2 Ostrich) that the test program allocated are now explicitly persisted in the transaction---and I'm still getting an exception:

Exception in thread "main" [ObjectDB 2.2.6_03] javax.persistence.RollbackException
Failed to commit transaction: Attempt to persist a reference to a non managed failing.Ostrich instance (error 613)
at com.objectdb.jpa.EMImpl.commit(
at failing.ODBTest.main(
Caused by: javax.persistence.PersistenceException: com.objectdb.o.UserException: Attempt to persist a reference to a non managed failing.Ostrich instance
at com.objectdb.o._PersistenceException.b(
at com.objectdb.o.JPE.g(
at com.objectdb.o.JPE.g(
... 4 more
Caused by: com.objectdb.o.UserException: Attempt to persist a reference to a non managed failing.Ostrich instance
at com.objectdb.o.MSG.d(
at com.objectdb.o.TYW.writeElement(
at failing.Index.__odbWriteContent(
at com.objectdb.o.UTY.aF(
at com.objectdb.o.UTY.aE(
at com.objectdb.o.ENH.a(
at com.objectdb.o.STA.T(
at com.objectdb.o.STM.D(
at com.objectdb.o.OBM.bH(
at com.objectdb.jdo.PMImpl.bH(
at com.objectdb.o.OBM.bG(
at com.objectdb.o.OBM.bE(
at com.objectdb.jpa.EMImpl.commit(
... 1 more

Any clues as to what's going on here? This is so insanely simple, I must be missing something really basic.


Thank you for this report and test case.

Please try build 2.2.6_04 that should fix this issue.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Code that was used for the fix had to be changed in build 2.2.8_06 because it causes some other problems.

If as a result of this the problems happens again - please report.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

It is still happening for many to many relationship.... 

Failed to commit transaction: Attempt to persist a reference to a non managed jpa_beans.TicketNumbers instance


Sorry for the wrong answer:

Here is the final sample:-


@ManyToMany(cascade = {CascadeType.ALL}, targetEntity = Passenger.class, mappedBy = "buses")

  private Set<Passenger> passengers= new HashSet();



@ManyToMany(cascade = { CascadeType.ALL }, targetEntity = Bus.class)
    private List<Bus> buses= new ArrayList();

Post Reply

To post a reply and/or subscribe to update notifications - please login