JPA2.0 How to map a collection of an entity with inheritance mapped by TABLE_PER_CLASS

172 Views Asked by At

I want to map a collection (a map) of an entity with inheritance. The entity and the inheriting entity look like that:

@Entity
public class TestEntity {

    @OneToMany(mappedBy = "parent")
    public Map<String, MapValueEntity> map 
        = new HashMap<String, MapValueEntity>();

    @Id
    @GeneratedValue(strategy = AUTO)
    protected long id;

    public String name;

    public TestEntity() {
    }

    public TestEntity(String name) {
        this.name = name;
    }
}

@Entity
public class SubEntity extends TestEntity{

    public SubEntity() {
    }

    String testSubEntityName;

    public SubEntity(String name) {
        this.testSubEntityName = name;
    }
}

The entity that is used as map value is this

@Entity
public class MapValueEntity {

    TestEntity parent;

    @Id
    @GeneratedValue(strategy = AUTO)
    protected long id;

    public MapValueEntity() {
    }

    String subEntityName;

    public MapValueEntity(String name) {
        this.subEntityName = name;
    }
}

And here is the test code:

EntityManager em = EntityManagerService.getEntityManager();
em.getTransaction().begin();

for (int i = 0; i < 10; i++) {
    TestEntity e = new TestEntity("MyNameIs" + i);
        for (int j = 0; j < 8; j++) {
            MapValueEntity se = new MapValueEntity("IamNo" + j + "." + i);
            e.map.put("Key" + i * 100 + j, se);
            se.parent = e;
            em.persist(se);
        }
        em.persist(e);
    }

em.getTransaction().commit();

So far everything works fine. EclipseLink puts the TestEntity and the SubEntity in one table with a discriminator column for distinguishing between the classes. And the complete map data is stored in the table for the MapValueEntity. Now if I change the mapping strategy from default (= SINGLE_TABLE) to TABLE_PER_CLASS the construction breaks:

@Entity
@Inheritance(strategy = TABLE_PER_CLASS)
public class TestEntity {
...

The exception description says:

Multiple writable mappings exist for the field [MAPVALUEENTITY.MAP_KEY]. Only one may be defined as writable, all others must be specified read-only.

Do you see any chance to use the TABLE_PER_CLASS strategy in this scenario? Where are mentioned the "multiple writable mappings"?

0

There are 0 best solutions below