简体   繁体   中英

I want to map a Map<Long, List<POJO>> through JPA

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 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

Use of @JoinTable.inverseJoinColumns targeting an unmapped class: ItemAttributeGroup.attributes[java.util.List]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM