0
我有一些麻煩得到HQL的掛鉤。我有一個應用程序,我在一張桌子上有一堆競爭對手。然後我有比賽,在那裏我匹配競爭對手。每個競賽可能有許多競爭對手。每個參賽者可能會參加多項比賽。 (事實上,我正在組織一場「循環賽」比賽,因此所有參賽者都將參加很多比賽,以確保他們能夠與其他所有參賽者一起比賽。)休眠多對多的映射和查詢去與它
我自己並沒有創建連接表使用Hibernate的XML來創建連接表。最後,我想我應該能夠編寫類似下面的HQL查詢:
這實際上是一個更大的查詢的一部分select count(*) from Competition c where c.competitor.id = :competitorId
簡單的查詢。我的意圖是計算給定參賽者參賽的比賽數量。我遇到的問題雖然是錯誤,如:
org.hibernate.QueryException: could not resolve property: competitor of: com.sodapopsoftware.imagewar.model.db.Competition [ select count(*) from com.sodapopsoftware.imagewar.model.db.Competition c where c.competitor.id = :competitorId ]
這裏是我的競爭和競爭對手的映射。
競爭
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.sodapopsoftware.imagewar.model.db.Competition" table="competition">
<meta attribute="use-in-tostring">true</meta>
<meta attribute="extra-import">java.util.Set</meta>
<meta attribute="extra-import">java.util.TreeSet</meta>
<id name="competitionId" column="competition_id" type="int">
<meta attribute="scope-set">protected</meta>
<meta attribute="field-description">Primary Key</meta>
<meta attribute="use-in-equals">true</meta>
<generator class="native" />
</id>
<many-to-one name="league" column="league_id" unique="false" not-null="true"
class="com.sodapopsoftware.imagewar.model.db.League"
/>
<property name="dateAdded" column="date_added" type="date" not-null="true">
<meta attribute="field-description">When the league was created</meta>
</property>
<set name="competitors" table="competition_competitors" sort="com.sodapopsoftware.imagewar.model.util.CompetitorIdComparator">
<meta attribute="property-type"><![CDATA[Set<Competitor>]]></meta>
<meta attribute="default-value"><![CDATA[new TreeSet<Competitor>()]]></meta>
<key column="competition_id"/>
<many-to-many column="competitor_id"
unique="false"
class="com.sodapopsoftware.imagewar.model.db.Competitor"/>
</set>
</class>
</hibernate-mapping>
COMPETITOR
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.sodapopsoftware.imagewar.model.db.Competitor" table="competitor">
<meta attribute="use-in-tostring">true</meta>
<meta attribute="extra-import">java.lang.Comparable</meta>
<meta attribute="implements">Comparable<![CDATA[<]]> Competitor <![CDATA[>]]></meta>
<meta attribute="class-code"> private static final long serialVersionUID = 1L;</meta>
<meta attribute="class-code">
public int compareTo(Competitor that) {
if (that != null) {
return this.getCompetitorId() - that.getCompetitorId();
}
return 1;
}
</meta>
<id name="competitorId" column="competitor_id" type="int">
<meta attribute="scope-set">protected</meta>
<meta attribute="use-in-equals">true</meta>
<generator class="native" />
</id>
<many-to-one name="league" column="league_id" unique="false" not-null="true"
class="com.sodapopsoftware.imagewar.model.db.League"
/>
<property name="name" column="name" type="string" not-null="true">
<meta attribute="field-description">Gives this competitor a name</meta>
</property>
<property name="nameNormalized" column="name_normalized" unique="true" type="string" not-null="true">
<meta attribute="field-description">Same as name, but lower case for searching</meta>
</property>
<property name="description" column="description" type="string">
<meta attribute="field-description">Describes this competitor</meta>
</property>
<property name="linkImageThumbnail" column="link_image_thumbnail" type="string" not-null="true">
<meta attribute="field-description">Link to the thumbnail image for this competitor</meta>
</property>
<property name="linkImageFullSized" column="link_image_full_sized" type="string" not-null="true">
<meta attribute="field-description">Link to the full-sized image for this competitor</meta>
</property>
<property name="dateAdded" column="date_added" type="date" not-null="true">
<meta attribute="field-description">When the competitor was created</meta>
</property>
</class>
</hibernate-mapping>
Do y當人們不斷低頭並感謝你時,你會發現它令人厭煩嗎? – Marvo 2012-03-20 04:57:13
一個「謝謝」是最好的獎勵,至少對我來說 – Firo 2012-03-20 05:36:43
謝謝!幫助了一堆。 – Marvo 2012-03-20 05:38:43