Question

I am using hibernate with PostgreSQL database. I have two entity beans Task and 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;
}

I need to sort the tasks by fullname of the user using a single hql query. Someone please help.

Was it helpful?

Solution

HQL Query

FROM Task AS t ORDER BY t.assignedPerson.fullName

HQL DOC

14.11. The order by clause

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top