我正在使用 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;
}
}
我的社会统计类是:
@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;
}
}