当只有userId可用时使用hql,如何按用户名排序

时间:2014-02-04 08:49:04

标签: hibernate postgresql hql

我正在使用带有PostgreSQL数据库的hibernate。我有两个实体bean Task和User。

@Entity
@Table(schema = TaskConstants.TASKSCHEMA, name = "tasks")
public class Task {
    @Id
    @Column(name = "id", columnDefinition = "serial")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "title")
    private String title;

    @Column(name="assigned_to")
    private int assignedTo;

    @ManyToOne(targetEntity = User.class, fetch = FetchType.EAGER)
    @JoinColumn(name = "assigned_to", referencedColumnName = "id", insertable = false, updatable = false)
    private User assignedPerson;
}

@Entity
@Table(schema = TaskConstants.TASKSCHEMA, name = "users")
public class User {
    @Id
    @Column(name = "id", columnDefinition = "serial")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "full_name")
    private String fullName;
}

我需要使用单个hql查询按用户的全名对任务进行排序。有人请帮忙。

1 个答案:

答案 0 :(得分:0)

HQL查询

FROM Task AS t ORDER BY t.assignedPerson.fullName

HQL DOC

<强> 14.11. The order by clause