本文介绍了我想映射一个 Map<Long, List<POJO>>通过 JPA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 @Entity 类中映射一个 Map>,其中 ItemAttribute 本身是一个 @Entity 单独定义.

I want to map a Map<Long, List<ItemAttribute>> inside an @Entity class where ItemAttribute itself is an @Entity defined separately.

这是我用于映射的代码:

Here is the code that I am using for mapping:

@Entity
@Table(name = "ITEM_ATTRIBUTE_GROUP")
public class ItemAttributeGroup implements Cloneable, Serializable
{
      @ElementCollection
      @MapKeyColumn(name="groupId")
      @JoinTable(name = "ATTRIBUTES_IN_GROUP", joinColumns = @JoinColumn(name = "groupId"),
                        inverseJoinColumns = @JoinColumn(name = "ID"))
      private Map<Long, List<ItemAttribute>> attributes = new HashMap<Long, List<ItemAttribute>>();
     //getters and setters........
}

ItemAttribute 是下面提到的一个单独的类:

ItemAttribute is a separate class mentioned below:

@Entity
@Table(name = "ITEM_ATTRIBUTE")
public class ItemAttribute implements Cloneable, Serializable {
    private static final long serialVersionUID = -8017036630979138942L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Long id;

    @ElementCollection // this is a collection of primitives
    @JoinTable(name="ATTRIBUTE_VALUE_RANGE", joinColumns=@JoinColumn(name="ID"))
    @MapKeyColumn (name="RANGE_ID")// column name for map "key"
    @Column(name="VALUE")//  column name for map "value"
    private Map<String, String> attributeValueRange = new HashMap<String, String>();

    @ElementCollection // this is a collection of primitives
    @JoinTable(name="QUALIFIER_VALUE_RANGE", joinColumns=@JoinColumn(name="ID"))
    @MapKeyColumn (name="RANGE_ID")// column name for map "key"
    @Column(name="VALUE")//  column name for map "value"
    private Map<String, String> qualifierValueRange = new HashMap<String, String>();

    public Map<String, String> getAttributeValueRange() {
        return attributeValueRange;
    }

    public void setAttributeValueRange(Map<String, String> attributeValueRange) {
        this.attributeValueRange = attributeValueRange;
    }

    public Map<String, String> getQualifierValueRange() {
        return qualifierValueRange;
    }

    public void setQualifierValueRange(Map<String, String> qualifierValueRange) {
        this.qualifierValueRange = qualifierValueRange;
    }
}

问题出在我无法识别的代码中.我收到此错误

And the problem is somewhere in my code which I am unable to identify.I am getting this error

针对未映射的类使用@JoinTable.inverseJoinColumns:ItemAttributeGroup.attributes[java.util.List]

推荐答案

需要创建一个中间类ItemAttributes.

You need to create an intermediate class ItemAttributes.

@Entity
public class ItemAttributes {

   @OneToMany
   private List<ItemAttribute> attributes;
}

@Entity
public class ItemAttributeGroup implements Cloneable, Serializable {

   @OneToMany
   private Map<Long, ItemAttributes> attributesMap;
}

这篇关于我想映射一个 Map<Long, List<POJO>>通过 JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 18:32