Hibernate 问题-“使用@OneToMany 或@ManyToMany 定位未映射的类" [英] Hibernate problem - "Use of @OneToMany or @ManyToMany targeting an unmapped class"

查看:30
本文介绍了Hibernate 问题-“使用@OneToMany 或@ManyToMany 定位未映射的类"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Hibernate Annotations 找到自己的脚,但我遇到了一个问题,希望有人能提供帮助.

I'm finding my feet with Hibernate Annotations and I've hit a problem I hope someone can help with.

我有 2 个实体,Section 和 ScopeTopic.该部分有一个 List 类成员,因此是一对多的关系.当我运行我的单元测试时,我收到了这个异常:

I have 2 entities, Section and ScopeTopic. The section has a List class member, so a One to Many relationship. When I run my unit test I am getting this exception:

针对未映射的类使用@OneToMany 或@ManyToMany:com.xxx.domain.Section.scopeTopic[com.xxx.domain.ScopeTopic]

Use of @OneToMany or @ManyToMany targeting an unmapped class: com.xxx.domain.Section.scopeTopic[com.xxx.domain.ScopeTopic]

我会假设错误意味着我的 ScopeTopic 实体没有映射到表?我看不出我做错了.以下是实体类:

I would assume that the error implies that my ScopeTopic entity isn't mapped to a table? I can't see with I have done wrong. Here are the Entity classes:

@Entity
public class Section {
    private Long id;
    private List<ScopeTopic> scopeTopics;

    public Section() {}

    @Id
    public Long getId() {
        return id;
    }

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

    @OneToMany
    @JoinTable(name = "section_scope", joinColumns = {@JoinColumn(name="section_id")},
               inverseJoinColumns = {@JoinColumn(name="scope_topic_id")} )
    public List<ScopeTopic> getScopeTopic() {
        return scopeTopic;
    }

    public void setScopeTopic(List<ScopeTopic> scopeTopic) {
        this.scopeTopic = scopeTopic;
    }
}

<小时>

@Entity
@Table(name = "scope_topic")
public class ScopeTopic {
    private Long id;
    private String topic;

    public ScopeTopic() {}

    @Id
    public Long getId() {
        return id;
    }

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

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }
}

<小时>

我很确定这是我自己缺乏理解的问题,所以一些指导会很好,谢谢!


I'm pretty sure it's my own lack of understanding that's at fault so some guidance would be great, thanks!

推荐答案

您的注释看起来不错.以下是需要检查的事项:

Your annotations look fine. Here are the things to check:

  • 确保注释是 javax.persistence.Entity,而不是 org.hibernate.annotations.Entity.前者使实体可检测.后者只是一个补充.

  • make sure the annotation is javax.persistence.Entity, and not org.hibernate.annotations.Entity. The former makes the entity detectable. The latter is just an addition.

如果您手动列出实体(在persistence.xml、hibernate.cfg.xml 或配置会话工厂时),请确保您还列出了ScopeTopic实体

if you are manually listing your entities (in persistence.xml, in hibernate.cfg.xml, or when configuring your session factory), then make sure you have also listed the ScopeTopic entity

确保在不同的包中没有多个 ScopeTopic 类,并且您导入了错误的类.

make sure you don't have multiple ScopeTopic classes in different packages, and you've imported the wrong one.

这篇关于Hibernate 问题-“使用@OneToMany 或@ManyToMany 定位未映射的类"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆