在JPQL中的递归函数内查询

时间:2016-06-08 09:43:06

标签: java hibernate spring-mvc jpa jpql

我是Jpa和jpql的新手,我需要从查询中生成下拉菜单。在codeigniter中很容易。我只是在递归函数中查询查询。

以下是样本

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>';
        }
    }

但现在我正在学习spring mvc,我需要生成下拉菜单。这是我尝试过的,根本不起作用。

这是我的Dao方法

@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;
}

我得到会话已关闭错误

  

org.hibernate.SessionException:会话已关闭!

这是我的类别代码

@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;
}

问题是:我无法通过打字查询实现我所需要的,有人请指导我如何在jpql / Jpa中实现这一点。我现在正在使用spring mvc,hibernate和mysql。

0 个答案:

没有答案