Hibernate注解多对一问题
我在保存具有一对多关系的对象时遇到一些问题。在我的问题中,一个 UserGrop 有许多 UserPermissions。对于这种关系,我创建了这样的域类:
@Entity
@Table(name = "tbl_usergroup")
public class UserGroup implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="userGroupId")
private long userGroupId;
@Column(name = "userGroupName")
private String userGroupName;
@OneToMany(cascade=CascadeType.ALL,mappedBy="userGroup",fetch=FetchType.EAGER)
private Set<UserPermissions> userPermissions = new HashSet<UserPermissions>(0);
}
@Entity
@Table(name = "tbl_group_permissions")
public class UserPermissions implements Serializable {
@Id
@GeneratedValue
private Long userPermissionId;
@ManyToOne(cascade =CascadeType.ALL)
@JoinColumn(name="userGroupId",nullable=false)
@ForeignKey(name = "userGroupId")
private UserGroup userGroup;
}
但是在保存 UserGroup 时,它也会保存 UserPermissions 对象。它与表没有任何关系(检索 UserGroup 对象时,它不返回 UserPermissions 对象集)。
DB:
+------------------+-------------+
--+
| userPermissionId | userGroupId |
e |
+------------------+-------------+
--+
| 33 | NULL |
|
| 34 | NULL |
|
| 35 | NULL |
|
| 36 | NULL |
|
| 37 | NULL |
|
| 38 | NULL |
|
| 39 | NULL |
|
| 40 | NULL |
|
+------------------+-------------+
--+
8 rows in set (0.00 sec)
有谁能帮我解决这个问题吗?
I have some issue with save object with one to many relationship. In my problem One UserGrop has many UserPermissions. Forthat relation ship I have create my domain class like this:
@Entity
@Table(name = "tbl_usergroup")
public class UserGroup implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="userGroupId")
private long userGroupId;
@Column(name = "userGroupName")
private String userGroupName;
@OneToMany(cascade=CascadeType.ALL,mappedBy="userGroup",fetch=FetchType.EAGER)
private Set<UserPermissions> userPermissions = new HashSet<UserPermissions>(0);
}
@Entity
@Table(name = "tbl_group_permissions")
public class UserPermissions implements Serializable {
@Id
@GeneratedValue
private Long userPermissionId;
@ManyToOne(cascade =CascadeType.ALL)
@JoinColumn(name="userGroupId",nullable=false)
@ForeignKey(name = "userGroupId")
private UserGroup userGroup;
}
But When saving UserGroup, it save hat UserPermisions Objects also. There with the table it does not have any relationship(when retrieving UserGroup object, it doesn't return Set of UserPermisions objects).
DB:
+------------------+-------------+
--+
| userPermissionId | userGroupId |
e |
+------------------+-------------+
--+
| 33 | NULL |
|
| 34 | NULL |
|
| 35 | NULL |
|
| 36 | NULL |
|
| 37 | NULL |
|
| 38 | NULL |
|
| 39 | NULL |
|
| 40 | NULL |
|
+------------------+-------------+
--+
8 rows in set (0.00 sec)
Can any body help me to solve this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
UserGroup.userPermissions
上的@OneToMany
中,您有mappedBy="userGroup"
。这意味着UserPermissions
中的userGroup
属性负责该关系。我猜你没有设置该属性,保存后它仍然为空。In the
@OneToMany
onUserGroup.userPermissions
you havemappedBy="userGroup"
. This means theuserGroup
property inUserPermisions
is responsible for the relation. I guess you don't set that property and upon saving it's still null.