我正在尝试按发布的时间戳按降序打印作者(id = 1)的帖子列表。我尝试使用以下两种方法:
//Query 1
Criteria topCriteria = session.createCriteria(Author.class)
.setFetchMode("posts", FetchMode.JOIN)
.add(Restrictions.idEq(1));
Criteria postsCriteria = topCriteria.createCriteria("posts", "P")
.addOrder(Order.desc("P.published"));
author = (Author) topCriteria.uniqueResult();
//Print author.getPosts()
//Query 2
author = (Author) session.createQuery("select A from Author A left join fetch A.posts as P where A.id=1 order by P.published desc").uniqueResult();
//Print author.getPosts()
实体类:
//Author.java
@Entity
public class Author {
@Id
@GeneratedValue
private int id;
private String firstname;
private String lastname;
@OneToMany(mappedBy = "author")
List<Post> posts;
//Getters and Setters omitted
}
//Post.java
@Entity
public class Post {
@Id
@GeneratedValue
private int id;
@JoinColumn(name="authorid")
@ManyToOne
private Author author;
private String content;
@Temporal(TemporalType.TIMESTAMP)
private Date published;
//Getters and Setters omitted
}
来自查询1的Author.posts未在已发布的列上排序。查询2包含按已发布列排序的帖子。这两种方法似乎都生成相同的SQL查询。
我需要在查询1中更改哪些内容以生成与查询2相同的输出?
Hibernate:4.3.8.Final