0

我有三个实体用户、中心和个人资料,所以一个用户有不同的个人资料,具体取决于他所在的中心。所以我们有一个表,它链接了三个实体,称为 *user_center_profile*。

然后我们有另一个实体,称为权限,它链接到个人资料,所以一个个人资料有几个权限。

因为主要实体是用户,所以我将其映射如下:

        @Entity
        @Table(name = "profile")
        public class Profile implements Serializable {

            @Id
            @GeneratedValue(strategy = GenerationType.AUTO)
            @Column(name = "idprofile")
            private Long idProfile;
            @Column(name = "codprofile")
            private String codProfile;
            @Column(name = "desprofile")
            private String desProfile;
            @OneToMany
            @JoinTable(name = "profile_permission", joinColumns = { @JoinColumn(name = "idProfile") }, inverseJoinColumns = { @JoinColumn(name = "idPermission") })
            private List<Permission> permissions = new ArrayList<Permission>();

            /* getters and setters */
        }

        @Entity
        @IdClass(CenterProfileId.class)
        @Table(name = "user_center_profile")
        public class CenterProfile implements Serializable {

            @Id
            @ManyToOne
            private Center center;
            @Id
            @ManyToOne
            private Profile profile;
            /* getters and setters */

        }

       @Embeddable
    public class CenterProfileId implements Serializable {

        private static final long serialVersionUID = 1L;
        @ManyToOne
        @JoinColumn(name = "idCenter")
        private Center center;
        @ManyToOne
        @JoinColumn(name = "idProfile")
        private Profile profile;

        /* getters, setters, equals, hashcode */
    }

   @Entity
@Table(name = "user")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "idUser")
    private Long idUser;
    @Column(name = "codUser")
    private String codUser;
    /* other properties */
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "user_center_profile", joinColumns = { @JoinColumn(name = "idUser") }, inverseJoinColumns = {
            @JoinColumn(name = "idCenter"), @JoinColumn(name = "idProfile") })
    private List<CenterProfile> centersProfileUser = new ArrayList<CenterProfile>();
    @Transient
    private Center selectedCenter;

    /* getters, setters */
}

问题是,在某些时候我必须收集某个用户拥有的所有权限......我尝试了几件事,但我遇到了延迟加载错误,没有会话或会话关闭错误,同时加载包时出现问题......

我什至尝试编写一个普通的 SQL 查询,但我得到了同样的错误......

我看不到为此构建 DetachedCriteria 的方法,也不知道它是否也会给出异常..

我的应用程序可以连接到不同的“中心”,当他登录时,他可以选择要连接的中心,而且一旦登录,他可以更改中心......所以当他改变它时,我必须重新计算他​​的权限...这就是为什么我需要获得该权限列表的原因..

我怎样才能以正确的方式完成这项工作?

4

1 回答 1

0

最后,我构建了这样的查询:

HashSet<Permission> set = new HashSet<Permission>();
for (CenterProfile cp : usuario.getCentersProfileUsuario()) {
    // First we build subcriteria to return all the permissions for a certain profile
    DetachedCriteria criteriaProfile = DetachedCriteria
            .forClass(Profile.class);
    criteriaProfile.add(Restrictions.eq("idProfile", cp.getProfile()
            .getIdProfile()));
    criteriaProfile.createAlias("permissions", "permission");
    criteriaProfile.setProjection(Property.forName("permission.idPermission"));
    // Then we build criteria for permissions, which should match the results given by subcriteria
    DetachedCriteria criteria = DetachedCriteria
            .forClass(Permission.class);
    criteria.add(Property.forName("idPermission").in(criteriaProfile));
    List<Permission> permissions = getHibernateTemplate().findByCriteria(
            criteria);
    set.addAll(permissions);
}
于 2013-06-19T08:52:52.970 回答