Hibernate中的@MappedSuperClass = AnnotationException:未知的mappedBy

时间:2012-02-27 13:25:45

标签: hibernate mappedsuperclass

错误: 引起:org.hibernate.AnnotationException:未知的mappedBy in:com.xxx.data.dao.classes.Categories.categoriesLang,引用的属性unknown:com.xxx.data.dao.classes.superclasses.CategoriesLang.categories < /强>

我的设置:

Hibernate 3.5.6

public interface CategoriesLangInt {

    int id_categories = 0;
    Categories categories = null;
    String name = "";
    String description = "";

    int getId_categories();

    void setId_categories(int id_categories);

    Categories getCategories();

    void setCategories(Categories categories);

    String getName();

    void setName(String name);

    String getDescription();

    void setDescription(String description);
}

@MappedSuperclass
public abstract class CategoriesLang implements CategoriesLangInt {

    @Id
    @Column(name = "id_categories")
    private int id_categories = 0;
    @OneToOne
    @JoinColumn(name = "id_categories")
    private Categories categories;
    @Size(max = 50)
    private String name = "";
    @Size(max = 350)
    private String description = "";

    @Override
    public int getId_categories() {
        return id_categories;
    }

    @Override
    public void setId_categories(int id_categories) {
        this.id_categories = id_categories;
    }

    @Override
    public Categories getCategories() {
        return categories;
    }

    @Override
    public void setCategories(Categories categories) {
        this.categories = categories;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public void setDescription(String description) {
        this.description = description;
    }

}

@Entity
@Table(name="categories")
public class Categories implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id_categories;
    private boolean active;
    @OneToOne(mappedBy="categories")
    private CategoriesLangInt categoriesLangInt;

    public Integer getId_categories() {
        return id_categories;
    }

    public void setId_categories(Integer id_categories) {
        this.id_categories = id_categories;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public CategoriesLangInt getCategoriesLangInt() {
        return categoriesLangInt;
    }

    public void setCategoriesLangInt(CategoriesLangInt categoriesLangInt) {
        this.categoriesLangInt = categoriesLangInt;
    }

}

更新解决了它!!!

经过很长一段时间的研究,我成功了。它工作正常!

EER

@Entity
@Table(name="categories")
public class Category implements Serializable {

    private Integer id;
    private boolean active;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

}

public interface CategoryInt{

    int getCategory_id();

    void setCategory_id(int category_id);

    String getName();

    void setName(String name);

    String getDescription();

    void setDescription(String description);

    void setCategory(Category category);

    Category getCategory();
}

@MappedSuperclass
public abstract class CategoryLang implements CategoryInt{

    private int category_id;
    private String name;
    private String description;
    private Category category;

    @Override
    @Id
    public int getCategory_id() {
        return category_id;
    }

    @Override
    public void setCategory_id(int category_id) {
        this.category_id = category_id;
    }

    @Override
    @Size(max = 50)
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    @Size(max = 350)
    public String getDescription() {
        return description;
    }

    @Override
    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    public Category getCategory() {
        return category;
    }

    @Override
    public void setCategory(Category category) {
        this.category = category;
    }
}

@Entity
@Table(name = "en_categories")
public class CategoryEN extends CategoryLang implements Serializable {}

@Entity
@Table(name = "es_categories")
public class CategoryES extends CategoryLang implements Serializable {}

2 个答案:

答案 0 :(得分:1)

首先,接口可能不定义字段,而只定义方法。您在界面中声明的字段实际上是公共静态最终常量。

之后,Categories实体有一个CategoriesLangInt字段,但Hibernate无法知道该字段代表哪个实体,因为可能有多个实体实现此接口。您必须使用OneToOne批注的targetEntity属性来告诉Hibernate引用了哪个实体。

最后,CategoriesLang中的id_categories列被映射两次:一次作为ID,一次作为JoinColumn。您必须删除id_categories字段并使用@Id注释categories字段,或保留id_categories字段,并将@MapsId注释添加到categories

附注:您应该尊重Java命名约定(字段和方法中没有下划线),并将类别重命名为Category,CategoriesLang重命名为CategoryLang等。

答案 1 :(得分:0)

我认为这是因为你的超类(接口,这里)中的字段被扩展类中类似命名的字段所遮蔽。尝试从界面中删除这些字段(如类别)。