0

我有一个用户实体,它被建模为

@Entity
class User {
    @ManyToMany
    @JoinTable(name="user_roles",
        joinColumns=@JoinColumn(
            name="user_id", referencedColumnName="id"),
        inverseJoinColumns=@JoinColumn(
            name="role_id", referencedColumnName="id")
        )
    public Set<Role> getRoles() {
        return roles;
    }
}

我想在分页视图中列出此用户特定角色。对于其他类,我正在使用这样的 HibernateTemplate

public PagedBean<User> findUsers(inal Status status, final PageParameters pageParameters) {
    return (PagedBean<User>) hibernateTemplate.execute(new HibernateCallback(){
    public Object doInHibernate(Session session) throws HibernateException, SQLException {
        Criteria crit = getUserByStatusCriteria(status, session);
        crit.addOrder(Order.asc("id"));
        Criteria critWithoutOrder = getUserByStatusCriteria(status, session);
        PagedResultBean<RssFeedSource> pagedResultBean = new PagedBean<User>(pageParameters);
        return populateFromCriteria(crit, critWithoutOrder, pagedBean);
    }

    private Criteria getUserByStatusCriteria(final Status status, Session session) {
        Criteria c = session.createCriteria(User.class);
        c.add(Restrictions.eq("status", status));
        return c;
    }});

}

对于不是实体的“user_roles”,如何实现与上述相同的分页结果?

4

1 回答 1

1

在休眠中:

Query q= session.createQuery("SELECT r FROM User u JOIN u.roles r WHERE u.id = :userId");
q.setString("userId", userId);
q.setMaxResults(10); // page size 10
q.setFirstResult(30); // page 4
List<?> roleList = q.list();
于 2013-11-14T13:45:26.437 回答