我想映射一个地图<long,list <pojo =“”>&gt;通过JPA </long,>

时间:2013-02-25 13:56:50

标签: java hibernate jpa annotations mapping

我想在Map<Long, List<ItemAttribute>>类中映射@Entity,其中ItemAttribute本身是@Entity单独定义的。

以下是我用于映射的代码:

@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是一个单独的类,如下所述:

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

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

  

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

1 个答案:

答案 0 :(得分:5)

您需要创建一个中间类ItemAttributes。

@Entity
public class ItemAttributes {

   @OneToMany
   private List<ItemAttribute> attributes;
}

@Entity
public class ItemAttributeGroup implements Cloneable, Serializable {

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