【发布时间】:2015-05-17 02:06:54
【问题描述】:
对不起我的英语。开始和javaee打交道,不太明白怎么做一些操作。我有 2 个表 categories 和 posts 。在 posts 中有字段 id、namecat和索引。在 posts 中有字段 id、namePost、text、idCat。如果表category index = 1。那么所有属于这个category的帖子都必须带到首页。在请求中它看起来像这样:
id = select * from category where index=1 //Here we learn how to id category i want displayed.
select * from post where idcat=id //and here all put
但我不知道如何在代码中做到这一点。在这里:
public Collection getPostFromCatId() {
List<Category> category= null;
Session session = null;
try{
session = HibernateUtil.getSessionFactory().openSession();
SQLQuery q = (SQLQuery) session.createSQLQuery("
select * from category where index=1"); //get all category i want displayed. How to get here id and paste the following query?
q.addEntity(Category.class);
category= q.list();
} catch(Exception e) { outputError(e);
} finally{ closeSession(session); }
return category;
}
【问题讨论】:
标签: java mysql hibernate servlets dao