Committe.java
public class Committe {
    private Integer id;
    private Users users;
    private String work;
    private String position;
    ...
}
Users.java
public class Users {
    private Integer id;
    private Integer userId;
    private String userName;
    ...
}
我想查询Committe中的work,position还有Users中的id,userId,userName。hql语句不用简单的from Committe,因为数据表中有比较大的数据这样的效率很低。单张表用select ... from ...的,多张表就不会写了。
各位大能,帮帮小弟吧。

解决方案 »

  1.   

    外键关联有的,Committe表中的User_Id字段与Users表中的id关联
      

  2.   

    用sql还是hql?
    用sql的话:select c.`work`, c.`position`, u.*
    from `committe` c
    left join `users` u
    on c.User_Id = u.id
      

  3.   

    实体类Committe中是没有User_Id的只有private Users users;查询语句中不能写where Committe.User_Id = Users.id 的吧?
      

  4.   


    试试where Committe.users.User_Id = Users.id
      

  5.   

    那个表后便跟的并不是表中的字段名称,而是类中的变量名
    org.springframework.orm.hibernate3.HibernateQueryException: could not resolve property: User_Id 
      

  6.   


    Committe.users.userId这样不行?
      

  7.   

    不行的,Committe后面只能跟着在实体类Committe.java中有的字段名
      

  8.   

    实在不行就createSqlQuery("
    select c.`work`, c.`position`, u.*
    from `committe` c
    left join `users` u
    on c.User_Id = u.id
    ")...
      

  9.   


    select a.work,a.position,a.users.id,a.users.userId,a.users.userName
    from Committe a
    where 1=1
    and ...
      

  10.   

    用Committe.users.?会出现如下错
    2013-3-6 15:27:02 org.apache.catalina.core.ApplicationDispatcher invoke
    严重: Servlet.service() for servlet jsp threw exception
    java.lang.NumberFormatException: For input string: "users"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)这是怎么回事呢?
      

  11.   

    查不到数据,为空。User_Id不能写吧。
      

  12.   

    select a.work,a.position,a.users.id,a.users.userId,a.users.userName
    from Committe a,a.users
    where 1=1
    and ... 要注意这样查返回的结果是一个对象数组,数组长度是查询的字段个数。
      

  13.   


    org.springframework.orm.hibernate3.HibernateQueryException: a.users is not mapped 
      

  14.   


    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "USER_ID")
    public Users getUsers() {
    return this.users;
    }public void setUsers(Users users) {
    this.users = users;
    }
    这个吧
      

  15.   

    嗯,这样写是没错的。没加a.users这个之前,你报的数据类型转换错误,不知道是不是跟你的取值有关。这样查返回结果是对象数组的集合,之后要迭代下
    类似于下面这样List<User> list = new ArrayList<User>();
    Iterator it=list.iterator();
    while(it.hasNext())
    {
    Object[] obj=(Object[])it.next();
    User user=new User();
    user.setName((String)obj[0]);
    user.setAge(Integer.parseInt(obj[1]));
    list.add(user);
    }
      

  16.   

    select * from sysobjects where parent_obj=(select id from sysobjects where [name]='你的表名') and xtype='F'
      

  17.   

    当然了  select *  是最不可取的  为了节省时间就这样写来了  望见谅