问答详情
源自:5-2 通过Object[]返回查询结果

select字句问题

为什么我和老师一样使用了select字句我查询的时候还是查询了其他列的数据,而且是给select字句指定的列名查询了两遍。代码如下

private static void testquery4(Session session) {
    String hql = "select s.name,s,tel,s.address,s.star from SellerEntity s";
    Query query = session.createQuery(hql);
    List<Object[]> list = query.list();
    for (Object[] objs:list){
        System.out.println("name:"+objs[0]);
        System.out.println("tel:"+objs[1]);
        System.out.println("address:"+objs[2]);
        System.out.println("star:"+objs[3]);
    }
}

输出如下

Hibernate: 
    select
        sellerenti0_.name as col_0_0_,
        sellerenti0_.Id as col_1_0_,
        sellerenti0_.tel as col_2_0_,
        sellerenti0_.address as col_3_0_,
        sellerenti0_.star as col_4_0_,
        sellerenti0_.Id as Id1_4_,
        sellerenti0_.name as name2_4_,
        sellerenti0_.tel as tel3_4_,
        sellerenti0_.address as address4_4_,
        sellerenti0_.website as website5_4_,
        sellerenti0_.star as star6_4_,
        sellerenti0_.business as business7_4_ 
    from
        seller sellerenti0_
name:A服装店
tel:yang.entity.SellerEntity@3518e16c
address:13000000000
star:中国北京××区
name:B数码店
tel:yang.entity.SellerEntity@6900a87f
address:15800000000
star:中国浙江杭州市××区
name:C电器店
tel:yang.entity.SellerEntity@dac41fd3
address:13012341234
star:中国广东深圳市××区
name:D书店
tel:yang.entity.SellerEntity@1096d97d
address:18600000000
star:中国陕西西安市××区

也就是按没有用select字句和加上select字句的结果都有

提问者:qq_何足道_1 2018-01-06 19:45

个回答

  • 黑店老板
    2018-06-02 22:13:42

    查询语句中写错了,应该是s.tel 你写的是s,tel 这样,你在输出tel的时候,实际输出的是整个对象SellterEntity,后面的输出依次往后推,就出现了你看到的结果了。

  • qq_纯粹_6
    2018-01-10 02:07:33

    不知道是不是有什么隐藏机制

  • qq_纯粹_6
    2018-01-10 02:06:59

    话说查询的语句中有其他列的属性,但是输出结果中没有其他属性,,这不科学啊


  • qq_纯粹_6
    2018-01-10 02:01:40

    看不懂你的问题不知道你在说什么