Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 地图<;字符串,字符串>;与JPA合作_Java_Hibernate_Jpa_Persistence - Fatal编程技术网

Java 地图<;字符串,字符串>;与JPA合作

Java 地图<;字符串,字符串>;与JPA合作,java,hibernate,jpa,persistence,Java,Hibernate,Jpa,Persistence,我有这样的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

我有这样的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;
    }
}
@实体
@表(name=“ATTRIBUTE\u GROUP”)
公共类属性组{
//身份证资料
@ElementCollection(fetch=FetchType.LAZY,targetClass=java.lang.String.class)
@CollectionTable(name=“ATTRIBUTE”)
@MapKeyColumn(name=“name”)
@列(name=“VALUE”)
私有映射属性=新HashMap();
public void createAttribute(字符串名称、字符串值){
属性=新属性();
attribute.setName(name);
属性。设置值(值);
attribute.setAttributeGroup(this);
attributes.put(名称、值);
}
公共映射getAttributes(){
返回属性;
}
}
我需要在
属性组
实体中有一个映射,它将
属性
的名称作为键,将
属性
的值作为值

目前的方法不适合我。当我试图将记录持久化到数据库时,它会生成一个异常,即事务标记为roolback only。我不知道这是否是一种写作方式,如果它不起作用,显然它不起作用

如何在JPA中实现这一点,使
AttributeGroup
中的映射由
Attribute
名称/值配对对象生成


我通过EntityManager使用Hibernate。

不能将元素集合与另一个实体存储在同一个表中,因为名称/值对不能既是元素又是实体。元素没有标识,并且不会维护任何其他字段

您可以改为使用1:M或M:M to属性,该属性使用名称作为映射键。然后,您可以将带有名称/属性对的映射直接公开给应用程序,或者创建其他访问器,在需要时将映射转换为映射。但是由于身份的原因,直接公开和使用属性可能是一个更好的主意


如果不需要这样做,则可以通过元素集合使用字符串/字符串对,但不能将同一个表作为实体或在其他元素集合映射中重用。该表将需要一个外键返回到AttributeGroup以及名称/值对的字段,如果重复使用,将导致问题。

是否必须使用hibernate注释?我们不能使用xml映射吗?是的,我需要坚持JPA方法,hibernate只是一个实现,因为我们将来可能会迁移到OpenJPA。发布异常的完整堆栈跟踪,以及导致异常的代码。还有,属性实体的意义是什么?你好像没用。您在方法中创建了一个新属性,但没有对其执行任何操作;是的,但是你的代码在那里什么都没有。创建了
attribute
的实例,您确实调用了
attribute.setAttributeGroup(this)
,但随后您什么也不做(例如,您不保存它),因此您所做的一切都将丢失。