0

我正在使用 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;
    }

}
4

1 回答 1

0

因为已经尝试了正确的映射,所以很明显@ElementCollection没有消耗注释。@ElementCollection与 JPA 2 一起引入。

JPA 2 API 存在,实现可能仍然缺失。较旧的 Hibernate 版本不是 JPA 2 实现。如果首选 3.X,那么最新的(3.6.10)值得尝试。

如果更新 Hibernate 版本不是一个选项,那么可以使用@CollectionOfElements 。

于 2013-05-16T04:39:19.057 回答