使用 mappedby 属性时的 Hibernate 异常
Hibernate Exception when using mappedby attribute
我正在尝试在 pojo 中的表之间进行映射 class.But 我收到一个属性的异常。
波乔 类
用户:
@Entity
@Table(name = "Users")
public class Users implements java.io.Serializable {
private int userId;
private Role role;
private Groups groupId;
private UserType userType;
private String userName;
private Boolean isActive;
public Users() {
}
public Users(int userId, Role role, Groups groupId ,UserType userType, String userName, boolean isActive) {
this.userId = userId;
this.role = role;
this.groupId = groupId;
this.userType = userType;
this.userName = userName;
this.isActive = isActive;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "UserID", unique = true, nullable = false)
public int getUserId() {
return this.userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "RoleID", nullable = false)
public Role getRole() {
return this.role;
}
public void setRole(Role role) {
this.role = role;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "GroupId", nullable = false)
public Groups getGroup() {
return this.groupId;
}
public void setGroup(Groups group) {
this.groupId = group;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "UserTypeID", nullable = false)
public UserType getUserType() {
return this.userType;
}
public void setUserType(UserType userType) {
this.userType = userType;
}
@Column(name = "UserName", nullable = false)
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column(name = "IsActive", nullable = false, columnDefinition = "BIT")
public Boolean isIsActive() {
return this.isActive;
}
public void setIsActive(Boolean isActive) {
this.isActive = isActive;
}
}
群组:
@Entity
@Table(name = "Groups")
public class Groups implements java.io.Serializable{
private int groupId;
private String groupName;
private String groupDesc;
private Set<Users> users = new HashSet<Users>(0);
public Groups (){
}
public Groups(int groupId, String groupName, String groupDesc){
super();
this.groupId = groupId;
this.groupName = groupName;
this.groupDesc = groupDesc;
}
public Groups(int groupId, String groupName, String groupDesc, Set<Users> users) {
super();
this.groupId = groupId;
this.groupName = groupName;
this.groupDesc = groupDesc;
this.users = users;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name ="GroupID", nullable = false)
public int getGroupId() {
return groupId;
}
public void setGroupId(int groupId) {
this.groupId = groupId;
}
@Column(name = "GroupName", nullable = false)
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
@Column(name = "GroupDesc", nullable = false)
public String getGroupDesc() {
return groupDesc;
}
public void setGroupDesc(String groupDesc) {
this.groupDesc = groupDesc;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "groupId") //Error
public Set<Users> getUserse() {
return this.userse;
}
public void setUserse(Set<Users> users) {
this.users = users;
}
}
我正在映射 Users class 的正确成员变量。但是我遇到了这样的异常
org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.project.pojo.Users.groupId in com.project.pojo.Groups.users
在用户 pojo 中,我有像角色、用户类型这样的属性,这些属性没有任何 exception.Whereas 组映射,我得到了这个异常。
有人可以帮我解决这个异常吗?
谢谢:)
一定是因为 users 和 userse 不同。
在您的 Groups
class 中,您首先将一个名为 users
的变量声明为一组 Users
,这很好。因此,如果您要在设置器上注释 @OneToMany
,则名称必须与变量名称相匹配。
此外,在您的 Users
table 中,您创建了一个 Groups
变量并将其命名为 groupId
,但在您的 Users
table 你有一个名为 groupId
的 int
类型,这会混淆休眠并导致问题。您应该将其重命名为:
private Groups group;
所以它应该是这样的:
@OneToMany(fetch = FetchType.LAZY, mappedBy = "group")
public Set<Users> getUsers() {
return this.users;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "group", nullable = false)
public Groups getGroup() {
return this.group;
}
你没有问,但你也需要修复你的 RoleID
table。我认为你还没有真正理解 hibernate ORM 是如何工作的,看这个:
http://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example-annotation/
您为用户 bean 中的组对象定义 getter 为:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "GroupId", nullable = false)
public Groups getGroup() {
return this.groupId;
}
这不是 Bean 的最佳访问器名称。
尝试将用户中的组对象从 groupID
重命名为 group
并将映射注释更改为类似以下内容:
@OneToMany(fetch = FetchType.LAZY, mappedBy = "group")
public Set<Users> getUserse() {
return this.userse;
}
你的classes用户和组的成员变量有问题
将groupId的变量名groupId改成Users.pojo的group,改成 mappedby attribute to group in groups class set method.
这将解决您的问题。
我正在尝试在 pojo 中的表之间进行映射 class.But 我收到一个属性的异常。 波乔 类
用户:
@Entity
@Table(name = "Users")
public class Users implements java.io.Serializable {
private int userId;
private Role role;
private Groups groupId;
private UserType userType;
private String userName;
private Boolean isActive;
public Users() {
}
public Users(int userId, Role role, Groups groupId ,UserType userType, String userName, boolean isActive) {
this.userId = userId;
this.role = role;
this.groupId = groupId;
this.userType = userType;
this.userName = userName;
this.isActive = isActive;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "UserID", unique = true, nullable = false)
public int getUserId() {
return this.userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "RoleID", nullable = false)
public Role getRole() {
return this.role;
}
public void setRole(Role role) {
this.role = role;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "GroupId", nullable = false)
public Groups getGroup() {
return this.groupId;
}
public void setGroup(Groups group) {
this.groupId = group;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "UserTypeID", nullable = false)
public UserType getUserType() {
return this.userType;
}
public void setUserType(UserType userType) {
this.userType = userType;
}
@Column(name = "UserName", nullable = false)
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column(name = "IsActive", nullable = false, columnDefinition = "BIT")
public Boolean isIsActive() {
return this.isActive;
}
public void setIsActive(Boolean isActive) {
this.isActive = isActive;
}
}
群组:
@Entity
@Table(name = "Groups")
public class Groups implements java.io.Serializable{
private int groupId;
private String groupName;
private String groupDesc;
private Set<Users> users = new HashSet<Users>(0);
public Groups (){
}
public Groups(int groupId, String groupName, String groupDesc){
super();
this.groupId = groupId;
this.groupName = groupName;
this.groupDesc = groupDesc;
}
public Groups(int groupId, String groupName, String groupDesc, Set<Users> users) {
super();
this.groupId = groupId;
this.groupName = groupName;
this.groupDesc = groupDesc;
this.users = users;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name ="GroupID", nullable = false)
public int getGroupId() {
return groupId;
}
public void setGroupId(int groupId) {
this.groupId = groupId;
}
@Column(name = "GroupName", nullable = false)
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
@Column(name = "GroupDesc", nullable = false)
public String getGroupDesc() {
return groupDesc;
}
public void setGroupDesc(String groupDesc) {
this.groupDesc = groupDesc;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "groupId") //Error
public Set<Users> getUserse() {
return this.userse;
}
public void setUserse(Set<Users> users) {
this.users = users;
}
}
我正在映射 Users class 的正确成员变量。但是我遇到了这样的异常
org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.project.pojo.Users.groupId in com.project.pojo.Groups.users
在用户 pojo 中,我有像角色、用户类型这样的属性,这些属性没有任何 exception.Whereas 组映射,我得到了这个异常。 有人可以帮我解决这个异常吗?
谢谢:)
一定是因为 users 和 userse 不同。
在您的 Groups
class 中,您首先将一个名为 users
的变量声明为一组 Users
,这很好。因此,如果您要在设置器上注释 @OneToMany
,则名称必须与变量名称相匹配。
此外,在您的 Users
table 中,您创建了一个 Groups
变量并将其命名为 groupId
,但在您的 Users
table 你有一个名为 groupId
的 int
类型,这会混淆休眠并导致问题。您应该将其重命名为:
private Groups group;
所以它应该是这样的:
@OneToMany(fetch = FetchType.LAZY, mappedBy = "group")
public Set<Users> getUsers() {
return this.users;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "group", nullable = false)
public Groups getGroup() {
return this.group;
}
你没有问,但你也需要修复你的 RoleID
table。我认为你还没有真正理解 hibernate ORM 是如何工作的,看这个:
http://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example-annotation/
您为用户 bean 中的组对象定义 getter 为:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "GroupId", nullable = false)
public Groups getGroup() {
return this.groupId;
}
这不是 Bean 的最佳访问器名称。
尝试将用户中的组对象从 groupID
重命名为 group
并将映射注释更改为类似以下内容:
@OneToMany(fetch = FetchType.LAZY, mappedBy = "group")
public Set<Users> getUserse() {
return this.userse;
}
你的classes用户和组的成员变量有问题 将groupId的变量名groupId改成Users.pojo的group,改成 mappedby attribute to group in groups class set method.
这将解决您的问题。