我正在尝试在pojo类中的表之间进行映射。但是我正在为一个属性获取异常。 Pojo类
用户:
@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类的正确成员变量。但我这样得到例外
org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.project.pojo.Users.groupId in com.project.pojo.Groups.users
在用户pojo中我有像role,usertype这样的属性,它们被映射而没有任何异常。对于组我正在获得此异常。 有人可以帮我解决这个例外。
谢谢:)
答案 0 :(得分:1)
在Groups
类中,您首先将名为users
的变量声明为Users
的一组,这很好。因此,如果要在setter上注释@OneToMany
,则名称必须与变量名称匹配。
此外,在您的Users
表格中,您创建了一个Groups
变量,并将其命名为groupId
,但在Users
表格中,您有一个int
表格groupId
}类型命名为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
您没有问,但您还需要修复extension String
{
public func substringWithRange(range: Range<Index.Distance>) -> String
{
return substringWithRange(startIndex.advancedBy(range.startIndex)..<startIndex.advancedBy(range.endIndex))
}
}
str.substringWithRange(index..<index + length)
表。我认为你真的不明白hibernate ORM是如何工作的,请看:
http://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example-annotation/
答案 1 :(得分:1)
类用户和组的成员变量存在问题 将变量名称 groupId 更改为 Users.pojo 的组,并将 mappedby 属性更改为组 in groups class set method。
这将解决您的问题。
答案 2 :(得分:0)
一定是因为用户与用户之间存在差异。
答案 3 :(得分:0)
您在Users bean中将Group对象的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;
}