有两张表
A表:id               name          age
B表:id            sid(id外键)我只想用B表的sid查询A表中的记录
我的hql该怎么写
Query q = s.createQuery(hql);
Iterator it =q.list().Iterator();
懂的讲讲下
谢谢

解决方案 »

  1.   

    String hql = "from A a where id="+ sid;(单个sid)
    String hql = "from A a where a.id in (select distinct b.sid from B b)";(B表内所有存在的sid)
      

  2.   

    String hql = "from A a where a.id="+ sid;(单个sid)
      

  3.   

    String sql =" select (a.*) from A a ,B b where A.id = B.sid";
    Query query = session.createSQLQuery(sql,"a",A.class);
      

  4.   

    我想要的是一种内连接或是其他连接
    而不是子查询
    这是我用SQL数据库生成的查询语句
    SELECT A.* FROM AINNER JOIN BON A.id= B.sid
    我换成HQL
    Query q = s.createQuery(FROM AINNER JOIN BON A.id= B.sid);
    Iterator it =q.list().Iterator();
    查询不出来记录
    不晓得是哪点搞错了
    谢谢楼上的几位朋友帮忙
      

  5.   

    如果你使用了hibernet的自动映射:String sql =" select (a.*) from A a ,B b where A.id = B.sid";
    变成hql为:"select a from A a,B b where a.id=b.a.id";这个不知是否满足你的要求String hql = "from A a right outer join a.b b";(B表内所有存在的sid)如果你没有使用hibernet的自动映射,也就是B表的外键对应的对象中的字段不是A类型:
    String hql = "from A a right outer join B b where a.id=b.sid";(B表内所有存在的sid)
      

  6.   

    如果使用了Hibernet的自动映射:
    String sql =" select (a.*) from A a ,B b where A.id = B.sid";
    转为Hql:   "select a from A a ,a.b b where a.id = a.b.sid";
    String hql = "select A a right outer join a.b b where a.id=b.a.id";如果没有使用Hibernet的自动映射,即在B对象中与sid对应的字段不是A类型:
    String sql =" select (a.*) from A a ,B b where A.id = B.sid";
    转为Hql:   "select a from A a ,B b where a.id = b.sid";
    String hql = "select A a right outer join B b where a.id=b.sid";
      

  7.   

    如果使用了Hibernet的自动映射:
    String sql =" select (a.*) from A a ,B b where A.id = B.sid";
    转为Hql:   "select a from A a ,a.b b where a.id = a.b.sid";
    String hql = "select A a right outer join a.b b where a.id=b.a.id";如果没有使用Hibernet的自动映射,即在B对象中与sid对应的字段不是A类型:
    String sql =" select (a.*) from A a ,B b where A.id = B.sid";
    转为Hql:   "select a from A a ,B b where a.id = b.sid";
    String hql = "select A a right outer join B b where a.id=b.sid";