本文介绍了我想映射一个Map< Long,List< POJO>>.通过JPA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在@Entity
类中映射Map<Long, List<ItemAttribute>>
,其中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
推荐答案
您需要创建一个中间类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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!