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

12,779

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).

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

Share:
12,779
Gergely Györki
Author by

Gergely Györki

Updated on June 25, 2022

Comments

  • Gergely Györki
    Gergely Györki almost 2 years

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

    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:

    @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".

    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.

    No other solutions on stackoverflow worked for me so far.

    Any idea what am i doing wrong?