我有这样的 JPA 实体:
@Entity
@Table(name = "ATTRIBUTE")
public class Attribute {
    //ID stuff
    @Column(name = "NAME", nullable = false)
    private String name;
    @Column(name = "VALUE", nullable = false)
    private String value;
    //getters and setters
}
和另一个实体:
@Entity
@Table(name = "ATTRIBUTE_GROUP")
public class AttributeGroup {
    //ID stuff
    @ElementCollection(fetch = FetchType.LAZY, targetClass = java.lang.String.class)
    @CollectionTable(name = "ATTRIBUTE")
    @MapKeyColumn(name = "NAME")
    @Column(name = "VALUE")
    private Map<String, String> attributes = new HashMap<>();
    public void createAttribute(String name, String value) {
        Attribute attribute = new Attribute();
        attribute.setName(name);
        attribute.setValue(value);
        attribute.setAttributeGroup(this);
        attributes.put(name, value);
    }
    public Map<String, String> getAttributes() {
        return attributes;
    }
}
我需要在AttributeGroup实体中有一个映射,它将Attribute' 名称作为键,Attribute' 值作为值。
目前的方法对我不起作用。当我尝试将记录持久保存到数据库时,它会生成事务仅标记为回滚的异常。我不知道它是否甚至是执行此操作的写入方式,如果它显然不起作用,那么它不是。
我怎样才能在 JPA 中实现这一点,以便AttributeGroup从Attribute名称/值配对对象中制作地图?
我正在通过 EntityManager 使用 Hibernate。