2013-02-25 58 views
0

我有POJO,我需要通过注释映射MAP字段。我正在尝试下面的代码。如何通过注释映射Map <String,String>?

@Entity 
@Table(name = "ITEM_ATTRIBUTE", catalog = "DataSync") 
public class ItemAttribute implements Cloneable, Serializable { 

    @ElementCollection(targetClass = AttributeValueRange.class) 
    @MapKeyColumn(name="rangeId") 
    @Column(name="value") 
    @CollectionTable(name="ATTRIBUTE_VALUE_RANGE", [email protected](name="ITEM_ID")) 
    private Map<String, String> attributeValueRange; 
} 

我也为Map域做了一个单独的类。 下面是AttributeValueRange

@Entity 
@Table(name="ATTRIBUTE_VALUE_RANGE", catalog="datasync") 
public class AttributeValueRange { 

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

    private String rangeId; 

    private String value; 

    /** 
    * @return the rangeId 
    */ 
    public String getRangeId() { 
     return rangeId; 
    } 

    /** 
    * @param rangeId the rangeId to set 
    */ 
    public void setRangeId(String rangeId) { 
     this.rangeId = rangeId; 
    } 

    /** 
    * @return the value 
    */ 
    public String getValue() { 
     return value; 
    } 

    /** 
    * @param value the value to set 
    */ 
    public void setValue(String value) { 
     this.value = value; 
    } 

    /** 
    * @return the id 
    */ 
    public Long getId() { 
     return id; 
    } 

    /** 
    * @param id the id to set 
    */ 
    public void setId(Long id) { 
     this.id = id; 
    } 
} 

我有错误

Caused by: org.springframework.orm.hibernate3.HibernateSystemException: could not get a field value by reflection getter of AttributeValueRange.id; nested exception is org.hibernate.PropertyAccessException: could not get a field value by reflection getter of AttributeValueRange.id 

Caused by: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of AttributeValueRange.id 

Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Long field AttributeValueRange.id to java.lang.String 

请确定缺少什么我在这里以下堆栈跟踪?

我使用ZK框架,Spring &休眠

回答

1

我做了如下修改映射得到它的权利:

@ElementCollection(targetClass = java.lang.String.class) 
@JoinTable(name="ATTRIBUTE_VALUE_RANGE", [email protected](name="ID")) 
@MapKeyColumn (name="RANGE_ID") 
@Column(name="VALUE") 
private Map<String, String> attributeValueRange = new HashMap<String, String>(); 

@JoinColumn表示实体类的ID字段包含该地图领域。

@MapKeyColumn代表地图的关键列。

@Column表示Map的值列。

@JoinTable表示将为该Map自动创建的表名称。

没有必要为该地图创建单独的实体类。

相关问题