我正在为一对多映射进行概念验证,但没有成功。我的架构如下:用户 -----> UserRole
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="UserRole")
public class UserRole {
private int userRoleId;
private String userRoleName;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "UserRoleId", unique = true, nullable = false)
public int getUserRoleId() {
return userRoleId;
}
public void setUserRoleId(int userRoleId) {
this.userRoleId = userRoleId;
}
@Column(name = "UserRoleName", unique = true, nullable = false)
public String getUserRoleName() {
return userRoleName;
}
public void setUserRoleName(String userRoleName) {
this.userRoleName = userRoleName;
}
}
这里是 User 类的一部分
@Entity
@Table(name="Users")
public class User {
private int userId;
private String firstName;
private String lastName;
private String userName;
private String password;
private String phone;
private String email;
private UserRole userRole;
private List<Meeting> meetings;
private List<MeetingInvitation> meetingInvitationsSend;
private List<MeetingInvitation> meetingInvitationsReceived;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "UserId", unique = true, nullable = false)
public int getUserId() {
return userId;
}
....
@ManyToOne(targetEntity=UserRole.class)
@JoinColumn (name="userRole")
public UserRole getUserRole() {
return userRole;
}
public void setUserRole(UserRole userRole) {
this.userRole = userRole;
}
}
我收到此错误 Initial SessionFactory creation failed.org.hibernate.AnnotationException: @OneToOne or @ManyToOne on User.userRole references an unknown entity: UserRole
你可以看到我在用户角色类中添加了@Entity 注释