我正在使用Hibernate和JPA注释来映射我的类。当hibernate试图映射这个类时,我遇到了问题
@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;
}
}
答案 0 :(得分:0)
由于尝试了正确的映射,因此很明显不会消耗@ElementCollection
注释。 JPA 2引入了@ElementCollection
。
存在JPA 2 API,实施可能仍然缺失。较旧的Hibernate版本不是JPA 2实现。如果首选3.X,则最新的(3.6.10)值得尝试。
如果不能更新Hibernate版本,则可以使用@CollectionOfElements。