JPA2.0如何使用TABLE_PER_CLASS映射的继承映射实体的集合

时间:2013-08-18 21:25:09

标签: java mapping jpa-2.0 table-per-class

我想映射具有继承的实体的集合(地图)。实体和继承实体看起来像这样:

@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;
    }
}

用作地图值的实体是

@Entity
public class MapValueEntity {

    TestEntity parent;

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

    public MapValueEntity() {
    }

    String subEntityName;

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

这是测试代码:

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();

到目前为止一切正常。 EclipseLink将TestEntity和SubEntity放在一个带有鉴别器列的表中,用于区分类。并且完整的地图数据存储在MapValueEntity的表中。 现在,如果我将映射策略从默认(= SINGLE_TABLE)更改为TABLE_PER_CLASS,则构造会中断:

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

例外描述说:

  

字段[MAPVALUEENTITY.MAP_KEY]存在多个可写映射。只有一个可以被定义为可写,所有其他的必须被指定为只读。

在这种情况下,您是否认为有机会使用TABLE_PER_CLASS策略?哪里提到“多重可写映射”?

0 个答案:

没有答案
相关问题