使用所询问实体的子实体的信息对 Hibernate Criteria 查询的结果进行排序
我有两个实体 Person 和 Book。系统中仅存储特定书籍的一个实例(添加书籍时,应用程序会在向数据库添加新行之前检查是否已找到该书籍)。实体的相关源代码可以在下面找到:
@Entity
@Table(name="persons")
@SequenceGenerator(name="id_sequence", sequenceName="hibernate_sequence")
public class Person extends BaseModel
{
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_sequence")
private Long id = null;
@ManyToMany(targetEntity=Book.class)
@JoinTable(name="persons_books", joinColumns = @JoinColumn( name="person_id"), inverseJoinColumns = @JoinColumn( name="book_id"))
private List<Book> ownedBooks = new ArrayList<Book>();
}
@Entity
@Table(name="books")
@SequenceGenerator(name="id_sequence", sequenceName="hibernate_sequence")
public class Book extends BaseModel
{
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_sequence")
private Long id = null;
@Column(name="name")
private String name = null;
}
我的问题是我想找到拥有特定人拥有的一些书籍的人。返回的人员列表应使用以下逻辑进行排序:拥有最多相同书籍的人应位于列表的第一个,列表中的第二个人拥有的书籍数量不如第一个人,但超过第三人称。执行此查询的方法的代码添加如下:
@Override
public List<Person> searchPersonsWithSimilarBooks(Long[] bookIds) {
Criteria similarPersonCriteria = this.getSession().createCriteria(Person.class);
similarPersonCriteria.add(Restrictions.in("ownedBooks.id", bookIds));
//How to set the ordering?
similarPersonCriteria.addOrder(null);
return similarPersonCriteria.list();
}
我的问题是,这可以通过使用 Hibernate 来完成吗?如果可以的话,该怎么做?我知道我可以实现一个比较器,但我更喜欢使用 Hibernate 来解决这个问题。
使用 SQL 时,我想要的结果可以通过使用以下 SQL 查询来实现:
select p.name, count(p.name) as bookCount from persons p, books b, person_book pb
where pb.person_id = p.id and pb.book_id = b.id and b.id in (1,2,3,4,5,6) group by
name order by bookCount desc
我一直在尝试找出如何将其转换为 Criteria 查询。我一直在尝试使用 Projections,但我似乎无法将结果映射回 Person 对象。
I have got two entities Person and Book. Only one instance of a specific book is stored to the system (When a book is added, application checks if that book is already found before adding a new row to the database). Relevant source code of the entities is can be found below:
@Entity
@Table(name="persons")
@SequenceGenerator(name="id_sequence", sequenceName="hibernate_sequence")
public class Person extends BaseModel
{
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_sequence")
private Long id = null;
@ManyToMany(targetEntity=Book.class)
@JoinTable(name="persons_books", joinColumns = @JoinColumn( name="person_id"), inverseJoinColumns = @JoinColumn( name="book_id"))
private List<Book> ownedBooks = new ArrayList<Book>();
}
@Entity
@Table(name="books")
@SequenceGenerator(name="id_sequence", sequenceName="hibernate_sequence")
public class Book extends BaseModel
{
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_sequence")
private Long id = null;
@Column(name="name")
private String name = null;
}
My problem is that I want to find persons, which are owning some of the books owned by a specific person. The returned list of persons should be ordered by using following logic: The person owning most of the same books should be at the first of the list, second person of the the list does not own as many books as the first person, but more than the third person. The code of the method performing this query is added below:
@Override
public List<Person> searchPersonsWithSimilarBooks(Long[] bookIds) {
Criteria similarPersonCriteria = this.getSession().createCriteria(Person.class);
similarPersonCriteria.add(Restrictions.in("ownedBooks.id", bookIds));
//How to set the ordering?
similarPersonCriteria.addOrder(null);
return similarPersonCriteria.list();
}
My question is that can this be done by using Hibernate? And if so, how it can be done? I know I could implement a Comparator, but I would prefer using Hibernate to solve this problem.
When using SQL the result I want can be achieved by using following SQL query:
select p.name, count(p.name) as bookCount from persons p, books b, person_book pb
where pb.person_id = p.id and pb.book_id = b.id and b.id in (1,2,3,4,5,6) group by
name order by bookCount desc
I have been trying to figure out how to translate this into the Criteria query. I have been trying to use Projections, but i seem to be unable to map the results back to the Person object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这还有用吗?
http://docs.jboss。 org/hibernate/core/3.3/reference/en/html/querycriteria.html#querycriteria-ordering
Is this of any use?
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querycriteria.html#querycriteria-ordering
我终于设法解决了这个问题。以下方法按预期工作:
我还通过添加一个名为 similarBookCount 的瞬态属性来更新我的 person 类,这在某些情况下可能很有用。
I finally managed to solve this problem. The following method works as expected:
I also updated my person class by adding a transient property called similarBookCount, which can be useful in some situations.