JPA manytomany单向映射更新问题

时间:2011-06-22 19:05:34

标签: hibernate jsf jpa

我有两个类Group和Permission,当我尝试向组添加新权限时,Group有一个Permission列表。我设置了新的权限列表并成功提交但没有在数据库中进行更新。我正在使用jpa 1( hibernate as provider)。我正在为每个entitymanager(而不是jta)创建请求。我管理bean请求范围(a4j:keepalive),我试着找到erro但不成功。

1 - 获取当前组以进行编辑从组g联接获取g.permissions中选择g,其中g.id =:id ,如果它来自延迟获取权限列表as here

2 - 从数据库刷新列表中的所有权限,然后合并组。

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 = "READABLE_ID", unique = true)
    private String readableId;

1 个答案:

答案 0 :(得分:0)

JPA 1有孤儿实体的问题,而hibernate的JPA-1实现jar有一些额外的错误。设置列表为null解决了我的问题。我希望JPA-2解决这个问题。