【发布时间】:2013-05-11 19:17:17
【问题描述】:
我正在使用 Hibernate 和 JPA 注释来映射我的类。当休眠尝试映射此类时我遇到了问题
@Entity
@Table(name = "social_item")
public class SocialItem extends Item {
private SocialSlot slot;
private Map<SocialStat, Integer> stats = new HashMap<SocialStat, Integer>();
@Enumerated(EnumType.STRING)
public SocialSlot getSlot() {
return slot;
}
public void setSlot(SocialSlot slot) {
this.slot = slot;
}
@MapKeyClass(value = SocialStat.class)
@ElementCollection(targetClass = Integer.class)
public Map<SocialStat, Integer> getStats() {
return stats;
}
public void setStats(Map<SocialStat, Integer> stats) {
this.stats = stats;
}
}
我的 socialStat 课程是:
@Embeddable
public enum SocialStat {
HAPPINESS
}
我收到了这个错误:
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Map, at table: social_item, for columns: [org.hibernate.mapping.Column(stats)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:269)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
at org.hibernate.mapping.Property.isValid(Property.java:185)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:440)
at org.hibernate.mapping.UnionSubclass.validate(UnionSubclass.java:40)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1108)
我猜这是因为我试图映射到一个基本类,但 @ElementCollection 注释不应该解决这个问题吗?
我的物品类是这样的:
package com.cabesoft.domain.model;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import com.cabesoft.domain.utils.Money;
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Item {
private Integer id;
private String name;
private String description;
private Integer requiredLevel;
private Money price;
public Item() {
}
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "oid")
public Integer getId() {
return id;
}
@Column(name = "name", nullable = false)
public String getName() {
return name;
}
@Column(name = "description", nullable = false)
public String getDescription() {
return description;
}
@Column(name = "required_level", nullable = false)
public Integer getRequiredLevel() {
return requiredLevel;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setDescription(String description) {
this.description = description;
}
public void setRequiredLevel(Integer requiredLevel) {
this.requiredLevel = requiredLevel;
}
@Embedded
public Money getPrice() {
return price;
}
public void setPrice(Money price) {
this.price = price;
}
}
【问题讨论】:
-
从枚举中移除@Embeddable注解并告诉我们它是否更好。
-
不,那没用,得到了同样的错误
-
同时移除 @MapKeyClass 注解和 ElementCollection 的 targetClass 属性。并添加一个 MapKeyEnumerated 注解来指定映射的键是必须存储为字符串还是序数。
-
我按照你说的做了,我的课程是这样结束的:@ElementCollection @MapKeyEnumerated(EnumType.STRING) public Map
getStats() { return stats; } 和我的社交统计类: public enum SocialStat { HAPPINESS } 仍然得到同样的错误 -
然后呢?解决问题了吗?
标签: hibernate jpa map annotations