I have two class Group and Permission ,Group has a list of Permission when i try do add new permission to a group.I set new List of permission and commit successfully but no update occur at database.I am using jpa 1 (hibernate as provider).I am creating request per entitymanager (not jta) .My managed bean request scope (a4j:keepalive) and I try this for locate erro but not successfull.
1-Getting current group for editing as Select g from Group g join fetch g.permissions where g.id= :id if it result from Lazy fetch of permission list(as here)
2-Refreshing all permission in list from database then merge group.
public class GroupEditorBean extends AbstractGroupBean {
private static final long serialVersionUID = -7454051588934099916L;
@PostConstruct
public void init() {
this.group= groupAccessor.findGroup(Long.valueOf(getParameter("group")));
perms = this.group.getPermIdList();
}
public String save() {
if(group.isRoot()){
addError("Root group can not be updated");
return "";
}
List<Permission> permissions= convertToPermission(perms);
this.group.setPermissions(permissions);
groupAccessor.merge(group);
logger.log(String.format("Updated group %s", group.getName()), "group",
Log.Severity.INFO);
return "modules/user/groups.xhtml";
}
@Entity
@Table(name = "GROUPS")
public class Group implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "GID")
private Long id;
@ManyToMany(cascade={CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
@JoinTable(name = "GROUP_PERMISSION", joinColumns = @JoinColumn(name = "GID"), inverseJoinColumns = @JoinColumn(name = "PID"))
private List<Permission> permissions;
@Entity
@Table(name = "PERMISSIONS")
public class Permission implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "PID")
private Long id;
@NotNull
@Column(name = "READA开发者_如何学GoBLE_ID", unique = true)
private String readableId;
JPA 1 has problem with orphan entities and JPA-1 implementation jar of hibernate have some extra bugs. Setting list to null solve my problem for this case .I hope JPA-2 solves this problems.
精彩评论