Query inside a recursive function in JPQL

2019-07-31 16:35发布

问题:

I am new to Jpa and jpql, I need to generate dropdown menu from a query. In codeigniter it was easy. I'd just do query inside a query in recursive function.

Here is the sample

function main_menu($base_id, $id=NULL, $class=NULL, $selected=NULL )
{
        $this->db->select('id, url, parent_id,title,');
        $this->db->where('parent_id',$base_id);
        $this->db->order_by('sort_order','asc');
        $query = $this->db->get('tbl_menu');

        if($query->num_rows()>0)
        {   
            if($this->level == 0) 
                echo "<ul class='".$class."' id='".$id."' >";
            else 
                echo '<ul>';

            foreach ($query->result() as $row)
            {       
                    $this->db->select('id,url,parent_id,title');
                    $this->db->where('parent_id',$row->id);
                    $this->db->order_by('sort_order','asc');

                    $query = $this->db->get('tbl_menu');

                    if($this->level == 0) 
                       echo "<li class='".$selected."'> ";  
                    else 
                       echo '<li>';

                    echo "<a href='".$row->url."'>".$row->title."</a>"; 

                    if($query->num_rows() > 0)
                    {               
                            $this->Common->main_menu($row->id , $id=NULL, $class=NULL, $selected=NULL  );   
                            $this->level++;     
                    }

                    echo '</li>';
                }

                echo '</ul>';
        }
    }

but now I am learning spring mvc and I need to generate dropdown menu. Here is what I have tried and it does not work at all.

This is my Dao method

@Repository
public class CategoryDao {

@PersistenceContext
private EntityManager entityManager;

private String menu = "";
public String GenerateCategoryMenu(Long parid){

    String query= "Select o FROM Categories As o WHERE o.parent_id = :parid";
    TypedQuery<Categories> q = entityManager.createQuery(query, Categories.class);
    q.setParameter("parid", parid);
    List<Categories> results = q.getResultList();
    if (!results.isEmpty()) {

        // writting on menu string
        this.menu = this.menu+"<ul>";
        Iterator<Categories> itr = results.iterator();

        while(itr.hasNext()) {
            Categories cat = itr.next();

            String query = "Select o FROM Categories As o WHERE o.parent_id = :par";
            TypedQuery<Categories> qb = entityManager.createQuery(query, Categories.class);
            qb.setParameter("par", cat.getId());
            List<Categories> childs = q.getResultList();

            // Concat String
            this.menu = this.menu+"<li><a href='http://localhost:8080/librarymanagementsystem/category/show/"+cat.getId()+"'>"+cat.getTitle()+"</a>";


            if(!childs.isEmpty()){
                GenerateCategoryMenu(cat.getId());
            }

            this.menu = this.menu+"</li>";

        }
        this.menu = this.menu+"</ul>";

    }

    return this.menu;
}

I get session is closed error

org.hibernate.SessionException: Session is closed!

Here is my category code

@Entity
@Table(name = "categories")
public class Categories {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;

private Long parent_id;

private String title;

private String url_title;

private String description;

private String created;

private String updated;

/* 
 * @ At the time of Creating new user
 * Auto persist created date
 * Auto persist updated for first time
 */
@PrePersist
protected void onCreate (){

    // Date conversion to string
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd h:mm:s");
    sdf.setLenient(false);
    String now = sdf.format(date);

    created = now;
    updated = now;


}


/*
 *  Respective Getter and Setter Methods
 *  
 */
public Long getId() {
    return id;
}public void setId(Long id) {
    this.id = id;
}

public Long getParent_id() {
    return parent_id;
}public void setParent_id(Long parent_id) {
    this.parent_id = parent_id;
}

public String getTitle() {
    return title;
}public void setTitle(String title) {
    this.title = title;
}

public String getUrl_title() {
    return url_title;
}public void setUrl_title(String url_title) {
    this.url_title = url_title;
}

public String getCreated() {
    return created;
}public void setCreated(String created) {
    this.created = created;
}

public String getUpdated() {
    return updated;
}public void setUpdated(String updated) {
    this.updated = updated;
}

public String getDescription() {
    return description;
}public void setDescription(String description) {
    this.description = description;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Categories other = (Categories) obj;
    if (id == null) {
        if (other.id != null)
            return false;
    } else if (!id.equals(other.id))
        return false;
    return true;
}

The problem is: I could not achieve what I needed with the typed query, somebody please guide me how to achieve this in jpql/Jpa. I am using spring mvc, hibernate and mysql right now.