Hierarchical selects. Parent-child relationship in the same table

Asked

Viewed 447 times

1

I have a Product Entity regarding @Manytoone. And I need to bring the last child record of each parent. I made a select in the table and this bringing what I need more I’m not right to do the same query with hql.

This is my select in mysql:

SELECT p1.id,p1.description,p1.code,p1.note,p1.owner_id FROM
product AS p1 LEFT JOIN product AS p2
ON p1.id = p2.owner_id
WHERE p2.owner_id IS NULL;

And this consultation and how I’m trying to do in the hql:

("SELECT E FROM " +  Product.class.getSimpleName()
                + " E LEFT JOIN " +  Product.class.getSimpleName()
                + " P ON E.id = P.owner_id"
                + " P WHERE P.owner_id IS NULL");

inserir a descrição da imagem aqui

1 answer

1

Try to change your hql to:

("SELECT E FROM " +  Product.class.getSimpleName() + " AS E"
    + " LEFT JOIN " +  Product.class.getSimpleName() + " AS P"
    + " ON E.id = P.owner_id"
    + " WHERE P.owner_id IS NULL");
  • Hello the AS makes no difference.

  • Yes, another option in place of AS is to put the name of the direct variable, but there is a personal criterion. "SELECT E FROM " + Product.class.getSimpleName() + " E"
 + " LEFT JOIN " + Product.class.getSimpleName() + " P"]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.