I'm not sure how to join tables in hibernate. Say i have a table called news
and another table as news_categories
. The news
table has a primary key as id, which also serves as a foreign key for table news_categories
. Now i need to find out the categories for each news
in the news
table, how can i do that in hibernate?
here is my code to get the news:
HibernateEntityManagerFactory hmf = (HibernateEntityManagerFactor开发者_如何学Pythony) _emf;
SessionFactory sf = hmf.getSessionFactory();
Session ss = sf.openSession();
ScrollableResults srq = ss.createCriteria(news.class)
.addOrder(Order.desc(("_creationDate")))
.setCacheMode(CacheMode.IGNORE)
.scroll(ScrollMode.FORWARD_ONLY);
..
on srq add .createAlias("newsCategories")
This way the the categories will be fetched together with the news items.
精彩评论