308 words

Issue #243 - Remove of an entry from a @OneToMany collection is not possible if the enhancer is disabled

BugVersion: Priority: NormalStatus: FixedReplies: 3
#1
2015-02-10 14:21

Hello!

When the JPA entities are not enhanced the removal of an entry from a @OneToMany collection is not possible. The following java code demonstrates the problem. The statement

parentEntity.getChildEntityContainer().remove("01");

does not work correctly with not enhanced JPA entities.

package objectdb.examples;
 
import java.util.HashMap;
import java.util.Map;
 
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Persistence;
 
import org.junit.Assert;
 
public final class ChildEntityRemoveProblem {
 
 
public static void main(String[] args) {
  EntityManagerFactory emf = Persistence.createEntityManagerFactory("objectdb:$objectdb/db/test.tmp;drop");
  EntityManager entityManager = emf.createEntityManager();
 
  ParentEntity parentEntity = new ParentEntity();
  entityManager.getTransaction().begin();
  entityManager.persist(parentEntity);
  entityManager.getTransaction().commit();
 
 
  entityManager.getTransaction().begin();
  ChildEntity childEntity = new ChildEntity();
  childEntity.setStringAttribute1("abc");
  parentEntity.getChildEntityContainer().put("01", childEntity);
  entityManager.flush();
 
  entityManager.remove(parentEntity.getChildEntityContainer().get("01"));
  parentEntity.getChildEntityContainer().remove("01");
  entityManager.flush();
 
  entityManager.getTransaction().commit();
 
  entityManager.clear();
 
  parentEntity = entityManager.find(ParentEntity.class, 1l);
  Assert.assertEquals(0, parentEntity.getChildEntityContainer().size());
 
  entityManager.close();
  emf.close();
}
 
 
@Entity
public static class ChildEntity {
 
  @Column
  private String stringAttribute1;
 
  public String getStringAttribute1() {
   return stringAttribute1;
  }
 
  public void setStringAttribute1(String stringAttribute1) {
   this.stringAttribute1 = stringAttribute1;
  }
}
 
@Entity
public static class ParentEntity {
 
  @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
  private Map<String, ChildEntity> childEntityContainer = new HashMap<String, ChildEntityRemoveProblem.ChildEntity>();
 
 
  public Map<String, ChildEntity> getChildEntityContainer() {
   return childEntityContainer;
  }
}
}

 

PG
PG's picture
Joined on 2012-06-28
User Post #19
#2
2015-02-11 12:04

Thank you for this report. It represents an issue with non enhanced classes, but it is not related to @OneToMany collection. Any change to entity objects after flush is not detected, as demonstrated by a simpler test case that also fails:

import javax.persistence.*;
 
import org.junit.Assert;
 
public final class I243b {
 
    public static void main(String[] args) {
        EntityManagerFactory emf =
            Persistence.createEntityManagerFactory(
                "objectdb:$objectdb/db/test.tmp;drop");
        EntityManager em = emf.createEntityManager();
 
        em.getTransaction().begin();
        MyEntity e = new MyEntity();
        em.persist(e);
        em.getTransaction().commit();
 
        em.getTransaction().begin();
        e.x = 1;
        em.flush();
 
        e.x = 2; // not detected if class is not enhanced
        em.flush();
 
        em.getTransaction().commit();
        em.clear();
 
        e = em.find(MyEntity.class, 1l);
        Assert.assertEquals(2, e.x);
 
        em.close();
        emf.close();
    }
 
    @Entity
    public static class MyEntity {
        int x;
    }
}

Build 2.6.0_04 includes a fix of this issue.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #2,135
#3
2015-02-12 07:49

Hello,

I have tried version 2.6.0_04. The problem with the @OneToMany collection is still not fixed.

PG
PG's picture
Joined on 2012-06-28
User Post #20
#4
2015-02-12 09:19

You are right, sorry. Somehow the fix has not been integrated into that build. Please try build 2.6.1.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #2,137

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Attachments:
Maximum file size: 32 MB
Cancel