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?
In the
@OneToManyonUserGroup.userPermissionsyou havemappedBy="userGroup". This means theuserGroupproperty inUserPermisionsis responsible for the relation. I guess you don’t set that property and upon saving it’s still null.