java 休眠“ManyToOne ...引用未知实体”异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34068659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:26:40  来源:igfitidea点击:

Hibernate "ManyToOne ... references an unknown entity" exception

javamysqlhibernateexceptionservlets

提问by Gergely Gy?rki

I just cannot get the relationship working between my two classes mapped to SQL tables with Hibernate.

我只是无法使用 Hibernate 将我的两个类之间的关系映射到 SQL 表。

The Role class:

角色类:

@Entity
@Table(name = "role")
public class Role {
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

    @Column(name="name")
    private String name;

    @OneToMany(mappedBy="memberinfo")
    private Set<Memberinfo> members;

    ...
}

And the Memberinfo class:

和 Memberinfo 类:

@Entity
@Table(name = "memberinfo")
public class Memberinfo {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id", nullable = false)
    private int id;

    @Column(name = "userid", nullable = false)
    private String userid;

    @Column(name = "email", nullable = false)
    private String email;

    @Column(name = "password", nullable = false)
    private String password;

    @Column(name = "salt", nullable = false)
    private String salt;

    @Column(name = "name", nullable = false)
    private String name;

    @Column(name = "address")
    private String address;

    @Column(name = "phonenum")
    private String phonenum;

    @ManyToOne(targetEntity=Role.class)
    @JoinColumn(name="role_id")
    private Role role;

    ...
}

When i try to fetch data from the DB, it connects, but throws an exception: "HTTP Status 500 - @OneToOne or @ManyToOne on model.Memberinfo.role references an unknown entity: model.Role".

当我尝试从数据库获取数据时,它连接,但抛出异常:“HTTP 状态 500 - @OneToOne 或 @ManyToOne on model.Memberinfo.role 引用了一个未知实体:model.Role”。

If i delete the variable "Role", then it works, and i can fetch the membership data, but i need the connection between the two tables, but in this case, the previously mentioned exception appears every time.

如果我删除变量“Role”,那么它就可以工作,并且我可以获取成员资格数据,但是我需要两个表之间的连接,但是在这种情况下,每次都会出现前面提到的异常。

No other solutions on stackoverflow worked for me so far.

到目前为止,stackoverflow 上没有其他解决方案对我有用。

Any idea what am i doing wrong?

知道我做错了什么吗?

回答by Giuseppe

The "unknown entity error" can be thrown if the class is not actually an Entity (not annotated whith javax.persistence @Entity) or if the persitence provider doesn't "know" the class (package not scanned).

如果该类实际上不是实体(未用 javax.persistence @Entity 注释)或者持久性提供程序不“知道”该类(未扫描包),则可能会抛出“未知实体错误”。

Is the Role class imported in Memberinfo the correct one ? Maybe you are importing another Role class from another library.

Memberinfo 中导入的 Role 类是否正确?也许您正在从另一个库中导入另一个 Role 类。