在Hibernate中获取OrderBy的ManyToOne关系

时间:2013-11-19 10:37:05

标签: java hibernate

我有2张桌子

1.用户

2.Company

每个用户都有一家公司。对于每个公司,它可以有多个用户。

UserBean.java

import java.io.Serializable;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Table(name = "tab_user")
public class UserBean implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    @Column(name = "user_id")
    private int user_id;
    @Column(name="user_login_pwd")
    private String user_login_pwd;


    @ManyToOne
    @JoinColumn(name="comp_id")
    private CompanyBean companyBean

我的CompanyBean是

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Table(name = "tab_company")

public class CompanyBean implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    @Column(name = "comp_id")
    private Integer comp_id;
    @Column(name = "comp_code")
    private String comp_code;
@OneToMany(mappedBy = "companyBean" , fetch = FetchType.EAGER,  cascade = CascadeType.ALL)
    @Fetch(value = FetchMode.SUBSELECT)
    private List<UserBean> companyUserList;

这是我的CompanyBean映射类。

现在我需要按&#39; comp_code&#39;显示用户排行列表。我的用户列表的DAO实现是

@SuppressWarnings("unchecked")
    @Override
    public List<UserBean> getUserList( String orderBy, String orderField) throws Exception{
        List<UserBean> userList = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            Criteria crit = session.createCriteria(UserBean.class);

            if(orderBy.equals("asc")){
                crit.addOrder(Order.asc(orderField));
            }else{
                crit.addOrder(Order.desc(orderField));
            }

            crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

            userList = crit.list();
        }finally{
            session.close();
        }
        return userList;
    }

那么如何通过usersList中的CompanyBean的comp_code获取订单?请帮忙。

2 个答案:

答案 0 :(得分:1)

没有Hibernate解决方案是创建您自己的Comparator并使用Collections.sort订购;

Hibernate解决方案正在使用@OrderBy注释。 Using hibernate with annotations, i want a one-many relationship to be sorted

答案 1 :(得分:1)

通过为bean创建别名,我们可以从manyToOne字段获取orderBy

@SuppressWarnings("unchecked")
            @Override
            public List<UserBean> getUserList( String orderBy, String orderField) throws Exception{
                List<UserBean> userList = null;
                Session session = HibernateUtil.getSessionFactory().openSession();
                try{
                    Criteria crit = session.createCriteria(UserBean.class,"user").createAlias("user.companyBean", "company");

                    /*If my order field is from company bean then it should be company.company_field*/
                    if(orderBy.equals("asc")){
                        crit.addOrder(Order.asc(orderField));
                    }else{
                        crit.addOrder(Order.desc(orderField));
                    }

                    crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

                    userList = crit.list();
                }finally{
                    session.close();
                }
                return userList;
            }