Mysql Hibernate多对多映射和查询

Mysql Hibernate多对多映射和查询,mysql,hibernate,database-design,hibernate-mapping,Mysql,Hibernate,Database Design,Hibernate Mapping,我很难掌握HQL的窍门。我有一个应用程序,其中一个表中有一群竞争对手。然后我会参加比赛,在那里我会与竞争对手比赛。每场比赛可能有任意数量的参赛者。每个参赛者可能参加多项比赛。(事实上,我正在建立一个“循环赛”竞赛,所以所有的参赛者都将参加很多比赛,以确保他们能与其他所有参赛者进行比赛。) 我尝试使用Hibernate的XML来创建联接表,而不是自己创建联接表。最终,我认为我应该能够编写如下HQL查询: select count(*) from Competition c where c.com

我很难掌握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>

真的
java.util.Set
java.util.TreeSet
受保护的
主键
真的
联盟成立时
]]>
()]]>
竞争对手

<?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>

真的
java.lang.com
可比竞争对手]]>
私有静态最终长serialVersionUID=1L;
公共int比较(竞争对手){
如果(该!=null){
返回this.getCompetitorId()-that.getCompetitorId();
}
返回1;
}
受保护的
真的
给这个竞争对手一个名字
与名称相同,但搜索时小写
描述此竞争对手
链接到此竞争对手的缩略图
链接到此竞争对手的全尺寸图像
当竞争对手创建时

您的查询对引用使用语法(manytone),但一对多需要显式连接才能为元素提供别名

select count(*)  from Competition c join c.competitors co where co.id = :competitorId

诸神,当人们不断地向你们鞠躬致谢时,你们会觉得厌烦吗?