简体   繁体   English

映射和查询此实体的最佳方法

[英]Best way to map and query this entities

I have three entities User, Center and Profile, so a user has different profiles depending on the center he's in. So we have a table which links the three entities called *user_center_profile*. 我有三个实体User,Center和Profile,因此,根据用户所在的中心,用户具有不同的配置文件。因此,我们有一个表,该表链接了三个实体,称为* user_center_profile *。

Then we have another entity, called Permission which is linked to Profile, so a profile has several permissions. 然后,我们有另一个称为Permission的实体,该实体链接到Profile,因此一个Profile具有多个权限。

Because the main entity is User, I have it maped like this: 因为主要实体是User,所以我将其映射为:

        @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 */
}

The thing is that at certain point I have to collect all the permissions that a certain user has... I tried several things and I got lazy load errors, no session or session closed errors, problem loading simultaneous bags... 问题是,在某些时候,我必须收集某个用户拥有的所有权限...我尝试了几件事,但出现了延迟加载错误,无会话或会话关闭错误,同时加载包的问题...

I even tried to write a plain SQL query and I got the same error... 我什至尝试编写一个普通的SQL查询,但遇到了同样的错误...

I can't see the way to build a DetachedCriteria for this, and I don't know if it will give an exception too.. 我看不到为此构建DetachedCriteria的方法,而且我也不知道它是否也会给出异常。

My app can connect to different "centers", when he logs in he can choose which center he wants to connect to, but also once logged in, he can change centers... So when he changes it, I have to recalculate his permissions... that's why I need to get that list of permissions.. 我的应用程序可以连接到不同的“中心”,当他登录时,他可以选择他想要连接的中心,但是一旦登录,他就可以更改中心...所以,当他更改它时,我必须重新计算他​​的权限这就是为什么我需要获得该权限列表的原因。

How could I get this done in a proper way? 我如何以适当的方式完成此工作?

In the end, I built the query like this: 最后,我建立了如下查询:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM