[英]Hibernate “ManyToOne … references an unknown entity” exception
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.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? 知道我做错了什么吗?
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). 如果类实际上不是实体(未注释whith javax.persistence @Entity)或者persitence提供者不“知道”类(包未扫描),则可以抛出“未知实体错误”。
Is the Role class imported in Memberinfo the correct one ? 在Memberinfo中导入的Role类是正确的吗? Maybe you are importing another Role class from another library.
也许您正在从另一个库导入另一个Role类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.