简体   繁体   English

休眠本机 sql 查询返回

[英]hibernate native sql query returned

I want to do a native SQL query, which joins 2 Tables.我想做一个本地 SQL 查询,它连接 2 个表。

The return value should be all elements of the District Table, which would be (id, name, district_code, coordinates) , and also a count(*) of all Objects which are in a District (therefore the join with some other table).返回值应该是 District 表的所有元素,这将是(id, name, district_code, coordinates) ,以及在一个District的所有对象的count(*) (因此与其他一些表连接)。

So I have all columns of district (district.*) one field which is Count(*) .所以我有区(district.*)一个字段的所有列,即Count(*) Which kind of query can I use, so that I can comfortable use it in my java code?我可以使用哪种查询,以便我可以在我的 Java 代码中轻松使用它? I can't add an entity or, because the count(*) wouldn't fit to it!?我无法添加实体,或者因为count(*)不适合它!?

I have got a District class which looks like this:我有一个 District 类,它看起来像这样:

@XmlRootElement
public class District extends AbstractEntity{

    private int id;
    private String name;
    private int district_code;
    @Transient
    private int carsQuantity;

    public District(){}

    @Override
    public int getId() {
        return id;
    }

    @Override
    public void setId(int id) {
        this.id = id;       
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getDistrict_code() {
        return district_code;
    }

    public void setDistrict_code(int district_code) {
        this.district_code = district_code;
    }

    public int getCarsQuantity() {
        return carsQuantity;
    }

    public void setCarsQuantity(int carsQuantity) {
        this.carsQuantity = carsQuantity;
    }
}

My District.hbm.xml:我的 District.hbm.xml:

<hibernate-mapping package="at.opendata.entitys">      
    <class name="District" table="districts">       
        <id name="id" column="id">
            <generator class="increment"/>
        </id>
        <property name="name"/>
        <property name="district_code"/>                
    </class>    
</hibernate-mapping>

EDIT:编辑:

I can't do it with HQL oder JPQL, because I need a SUBSELECT in my FROM CLAUSE.我不能用 HQL 或 JPQL 来做,因为我的 FROM CLAUSE 中需要一个 SUBSELECT。

SELECT d.id, count(*) FROM (SELECT cd.coordinates AS coordinates FROM cars AS c LEFT JOIN cardetail AS cd ON (c.car_id = cd.car_id)) AS c CROSS JOIN districts AS d WHERE ST_CONTAINS(d.coordinates, c.coordinates) GROUP BY id

First of all, you don't need a native query for that.首先,您不需要本机查询。 JPQL will do just fine. JPQL 会做得很好。 And the result of such a query will simply be a List<Object[]> , where each object array will contain an element for each returned column.这种查询的结果将只是一个List<Object[]> ,其中每个对象数组将包含每个返回列的元素。 Just iterate over this list and do whatever you want with the elements:只需迭代这个列表,然后对元素做任何你想做的事情:

for (Object[] row : list) {
    Integer id = (Integer) row[0];
    String name = (String) row[1];
    ...
    Long count = (Long) row[4];
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM