Java JPQL中递归函数内部的查询
我不熟悉Jpa和jpql,我需要从查询中生成下拉菜单。在codeigniter中,这很容易。我只需要在递归函数中的查询中执行查询 这是样品Java JPQL中递归函数内部的查询,java,hibernate,spring-mvc,jpa,jpql,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',$bas
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>';
}
}
问题是:我无法用类型化查询实现我所需要的,请有人指导我如何在jpql/Jpa中实现这一点。我现在正在使用spring mvc、hibernate和mysql。我认为您的entityManager/会话在启动getResultList之前的某个时刻已经关闭。代码中不清楚如何注入entityManager。检查这个。是的,如果类别是一个实体,您可以执行这些查询。你能复制你的分类代码吗?我认为您不需要在while循环和递归调用中明确地编写查询,让Hibernate通过遍历第一个恢复的对象关系来为您完成。另一方面,我不是Spring专家,我需要更多的时间来研究注入的entityManager会发生什么。也许其他人可以在这个问题上帮助你。我刚刚添加了我的类别代码,请你检查一下。我刚刚解决了这个问题,只是在我的generateCategoryMenu(长id)中添加了@transactional。我想你的entityManager/会话在你启动getResultList之前的某个时候已经关闭了。代码中不清楚如何注入entityManager。检查这个。是的,如果类别是一个实体,您可以执行这些查询。你能复制你的分类代码吗?我认为您不需要在while循环和递归调用中明确地编写查询,让Hibernate通过遍历第一个恢复的对象关系来为您完成。另一方面,我不是Spring专家,我需要更多的时间来研究注入的entityManager会发生什么。也许其他人可以在这个问题上帮助你。我刚刚添加了我的类别代码,请你检查一下。我刚刚解决了它,只是在我的GenerateCategory菜单(长id)中添加了@transactional。
@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;
}
@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;
}