我只是无法使用Hibernate将我的两个类映射到SQL表之间的关系.
角色类:
@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 Setmembers; ... }
和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; ... }
当我尝试从数据库中获取数据时,它会连接,但会引发异常:"HTTP状态500 - 模型上的@OneToOne或@ManyToOne.Memberinfo.role引用未知实体:model.Role".
如果我删除变量"Role",那么它可以工作,我可以获取成员资格数据,但我需要两个表之间的连接,但在这种情况下,每次都会出现前面提到的异常.
到目前为止,stackoverflow上没有其他解决方案对我有用.
知道我做错了什么吗?
如果类实际上不是实体(未注释whith javax.persistence @Entity)或者persitence提供者不"知道"类(包未扫描),则可以抛出"未知实体错误".
在Memberinfo中导入的Role类是正确的吗?也许您正在从另一个库导入另一个Role类.