列出由给定字段hibernate排序的数据库中的实体

时间:2014-11-22 15:29:48

标签: java hibernate sorting

我开始学习Hibernate,并且我试图从postgresql数据库中列出实体。我有一张桌子" dbuser"。然后我有Dbuser类的映射。

@Entity
@Table(name = "dbuser", schema = "public", catalog = "postgres")
public class DbuserEntity {
private int id;
private String username;
private String createdBy;
private Date createdDate;
private String firstName;
private String lastName;

@OrderBy("firstName")
public List<DbuserEntity> dbusers = new ArrayList<DbuserEntity>();

//constructors

@Id
@SequenceGenerator(name="pk_sequence",sequenceName="entity_id_seq", allocationSize=1)
@GeneratedValue(strategy=GenerationType.AUTO,generator="pk_sequence")
@Column(name = "id")
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

@Basic
@Column(name = "username")
public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

// other getters and setters, equals etc.

然后我在类Service中有方法从表中读取所有实体:

    public List readAll() {
    HibernateUtils hibernateUtils = new HibernateUtils();
    Session session = null;
    List users = new ArrayList<DbuserEntity>();
    try {
        session = hibernateUtils.getSessionFactory().openSession();
        session.beginTransaction();
        users = session.createQuery("FROM DbuserEntity ").list();

        session.beginTransaction().commit();
    } catch (HibernateException e) {
        session.beginTransaction().rollback();
        e.printStackTrace();
    } finally {
        if (session != null) {
            if (session.isOpen()) {
                session.close();
            }
        }
    }
    return users;
}

在main()方法中,我列出了表中的所有实体。我希望它按firstName排序。

      public static void main(String[] args) {   
      Service service = new Service();
      DbuserEntity db = new DbuserEntity();
      List users = new ArrayList<DbuserEntity>();
      users = service.readAll();
      db.dbusers = users;
      for (DbuserEntity dbuser : db.dbusers) {
        System.out.println(dbuser.toString());
    }

 }
 }

这会打印出来自表格的所有dbusers。

  DbuserEntity{id=19, username='user2', createdby='admin', createddate=2014-11-19, firstname='john', lastname='johnson'}

  DbuserEntity{id=20, username='user3', createdby='admin', createddate=2014-11-19, firstname='ivan', lastname='ivanov'}

  DbuserEntity{id=26, username='newUSer', createdby='system', createddate=2014-11-21, firstname='AAA', lastname='BBB'}

但他们没有排序。我不明白我做错了什么。我应该在代码中更改哪些打印实体排序?

3 个答案:

答案 0 :(得分:0)

createQuery("FROM DbuserEntity order by firstName").list();

生活可以很容易;-)

答案 1 :(得分:0)

试试这个

public List readAll() {
    HibernateUtils hibernateUtils = new HibernateUtils();
    Session session = null;
    List users = new ArrayList<DbuserEntity>();
    try {
        session = hibernateUtils.getSessionFactory().openSession();
        session.beginTransaction();
        users = session.createQuery("FROM DbuserEntity D ORDER BY D.FirstName ASC").list(); // Here I make changes

        session.beginTransaction().commit();
    } catch (HibernateException e) {
        session.beginTransaction().rollback();
        e.printStackTrace();
    } finally {
        if (session != null) {
            if (session.isOpen()) {
                session.close();
            }
        }
    }
    return users;
}

答案 2 :(得分:0)

您可以使用条件来构建查询。

Criteria criteria = session.createCriteria(DbuserEntity.class).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
criteria.addOrder(Order.asc("firstName"));
List list = criteria.list();

session.beginTransaction().commit();