hibernate关于多对多映射和多对一的hql查询
Posted acefeng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate关于多对多映射和多对一的hql查询相关的知识,希望对你有一定的参考价值。
public void queryCustomerbymerchantId(){ SessionFactory sf = HibernateUtil.getSessionFactory(); Session session = sf.getCurrentSession(); Transaction ts = session.beginTransaction(); try { Query query=session.createQuery("select c from Customer c join c.merchant m where m.mid=1"); List customer = query.list(); for(int i=0;i<customer.size();i++) { Customer customers =(Customer)customer.get(i); System.out.println(i+"--"+customers); } ts.commit(); } catch (HibernateException e) { // TODO Auto-generated catch block if(ts != null) { ts.rollback(); } e.printStackTrace(); } }
public void queryCustomerbyOrderId(){ SessionFactory sf = HibernateUtil.getSessionFactory(); Session session = sf.getCurrentSession(); Transaction ts = session.beginTransaction(); // int n; // Scanner sc=new Scanner(System.in); // n=sc.nextInt(); try { Query query=session.createQuery("select customer from Order o where o.id=1" ); System.out.println(query); List customer = query.list(); for(int i=0;i<customer.size();i++) { Customer customers =(Customer)customer.get(i); System.out.println(i+"--"+customers); } ts.commit(); } catch (HibernateException e) { // TODO Auto-generated catch block if(ts != null) { ts.rollback(); } e.printStackTrace(); } }
以上是关于hibernate关于多对多映射和多对一的hql查询的主要内容,如果未能解决你的问题,请参考以下文章