Pregunta

I am not sure how would this MySQL query look in JPQL. DETAILS:

select title
from post
order by (
  select count(postId)
  from comment
  where comment.postId=post.id
) desc;

EDIT: Post table look:

mysql> desc post;
+---------------+----------------+------+-----+---------+----------------+
| Field         | Type           | Null | Key | Default | Extra          |
+---------------+----------------+------+-----+---------+----------------+
| post_id       | int(11)        | NO   | PRI | NULL    | auto_increment |
| post_content  | varchar(50000) | NO   |     | NULL    |                |
| post_date     | datetime       | NO   |     | NULL    |                |
| post_summary  | varchar(1000)  | YES  |     | NULL    |                |
| post_title    | varchar(300)   | NO   |     | NULL    |                |
| post_visitors | int(11)        | NO   |     | NULL    |                |
| user_id       | int(11)        | NO   | MUL | NULL    |                |
| category_id   | int(11)        | NO   | MUL | NULL    |                |
+---------------+----------------+------+-----+---------+----------------+

Comment table look:

mysql> desc comment;
+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| comment_id      | int(11)      | NO   | PRI | NULL    | auto_increment |
| comment_content | varchar(600) | NO   |     | NULL    |                |
| comment_date    | datetime     | NO   |     | NULL    |                |
| comment_title   | varchar(300) | NO   |     | NULL    |                |
| user_id         | int(11)      | NO   | MUL | NULL    |                |
| post_id         | int(11)      | NO   | MUL | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+

This is command in mysql, terminal.

mysql> select post_title from post order by (select count(post_id) from comment where comment.post_id=post.post_id) desc;

I tried this, but its not working:

SELECT p FROM Post p ORDER BY 
(SELECT c COUNT(c.getPost().getId()) 
from Comment c 
where c.getPost().getId()=p.getId()) 
desc
¿Fue útil?

Solución 3

I have used native SQL for getting result I need. So, its not really an answer to my question, but I will post what I did, and what works (with plain SQL). Because, its not realized with JPQL, there isn't named query in an entity class, but native query has been used (inside the method of DAO object).

So, this is method content:

public List<Post> getMostCommentedPosts(){
    Query q = em.createNativeQuery("select * from post order by "
            + "(select count(post_id) from comment where comment.post_id=post.post_id) desc", Post.class);
    List<Post> resultList = (List<Post>) q.getResultList();

    if (resultList.isEmpty())
        return null;
    else
        return resultList;
}

Otros consejos

Main problem with non-working approach is calling methods of Java classes from JPQL query - that is not expected to work.

Assuming that significant properties of entities are roughly as follows:

@Entity
public class Post {
    @Id int id;
    @OneToMany (mappedBy = "post") List<Comment> comments;
    //...
}

@Entity
public class Comment {
    @Id int id;
    @ManyToOne Post post;
    //...
}

Then following should be enough with Hibernate+MySQL combo:

SELECT p 
FROM Post p 
ORDER BY SIZE(p.comments) DESC

JPA 2.0 specification requires bit more complex query, because what can be used in ORDER BY clause is rather limited:

  1. A state_field_path_expression that evaluates to an orderable state field of an entity or embeddable class abstract schema type designated in the SELECT clause by one of the follow- ing: • a general_identification_variable • a single_valued_object_path_expression
  2. A state_field_path_expression that evaluates to the same state field of the same entity or embeddable abstract schema type as a state_field_path_expression in the SELECT clause
  3. A result_variable that refers to an orderable item in the SELECT clause for which the same result_variable has been specified. This may be the result of an aggregate_expression, a scalar_expression, or a state_field_path_expression in the SELECT clause.

Following query should work also with other implementations. It is of course little bit tedious that additional variable is part of the result then:

SELECT p, SIZE(p.comments) as ord  
FROM Post p 
ORDER BY ord DESC

I don't use JPQL, but maybe this will work (you can also use it in MySQL):

SELECT post_title, COUNT(c.post_id) AS c
FROM Post p
LEFT JOIN Comment c
ON p.post_id = c.post_id
ORDER BY c
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top