这是我的问题:
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>
拍卖类映射:
<?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>
我需要使用hibernate创建相同的查询。我需要从每次拍卖中获取名称形式auc表,字段auction_id和字段数量的最大值。
现在查询:SELECT b FROM DAO.Bid b INNER JOIN b.auc_id
但它抛出错误,Auc表的第一个colomn在类Bid.Its中未映射,因为这个字段在类Auction.But我能做什么机智命中?
答案 0 :(得分:0)
HQL不是SQL。 HQL中的联接很大程度上利用了您的关系来正确映射,而不是每次都通过列名明确地加入。
假设您的模型如下:
class Auction {
Integer id;
String name;
}
class Bid {
Integer id;
Auction auction;
BigDecimal amount;
}
它看起来应该是
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
(auction.id
和auction.name
是隐式内部联接,表示bid.auction.id
)