简体   繁体   English

我怎么能在休眠中创建这样的查询

[英]How could i create such query in hibernate

Here is my query:这是我的查询:

SELECT auc_id,name,MAX(amount) FROM `auc`
INNER JOIN bid
where bid.auc_id = auc.id
GROUP BY (auc_id)

Mappig:映射:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="DAO.Bid" table="bid" lazy="false">

   <id column="id" name="id" type="java.lang.Integer"> 
   <generator class="identity"/>
  </id>
  <property column="amount" name="amount" type="java.lang.Integer" />
  <property column="type" name="type" type="java.lang.Integer" />
  <many-to-one name="owner_id" column="owner_id" class="DAO.Users" not-null="false" cascade="all"/>
  <many-to-one name="auc_id" column="auc_id" class="DAO.Auction" not-null="false" cascade="all"/>
  </class>
</hibernate-mapping>

Auction class mapping:拍卖类映射:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="DAO.Auction" table="auc" lazy="false">

  <id column="Id" name="id" type="java.lang.Integer"> 
   <generator class="identity"/>
  </id>
  <property column="name" name="name" type="java.lang.String" />
  </class>
</hibernate-mapping>

I need to create the same query with hibernate.I need to get name form auc table, fields auction_id and max value of field amount from each auction.我需要使用 hibernate 创建相同的查询。我需要从每次拍卖中获取名称表单 auc 表、auction_id 字段和字段金额的最大值。

Query as it is now : SELECT b FROM DAO.Bid b INNER JOIN b.auc_id But it throws error thet first colomn of Auc table is unmapped in class Bid.Its true because this field is inside class Auction.But what can i do wit hit?现在查询: SELECT b FROM DAO.Bid b INNER JOIN b.auc_id但是它抛出错误 Auc 表的第一列在类 Bid 中未映射。这是真的,因为这个字段在类 Auction 内。但是我能做什么打?

HQL is NOT SQL. HQL 不是 SQL。 Joins in HQL largely leverage on your relationships to be properly mapped, instead of doing joining explicitly by column names every time. HQL 中的联接在很大程度上利用您的关系进行正确映射,而不是每次都通过列名显式联接。

Assume you have your models like this:假设你有这样的模型:

class Auction {
    Integer id;
    String name;
}

class Bid {
    Integer id;
    Auction auction;
    BigDecimal amount;
}

it should looks like它应该看起来像

select auction.id, auction.name, max(amount) -- you may need to use max(auction.id), 
                                             -- max(auction.name) for some DB
from Bid
group by auction.id

(the auction.id and auction.name is an implicit inner join which means bid.auction.id ) auction.idauction.name是一个隐含的内部连接,这意味着bid.auction.id

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

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