Hibernate多对多映射和查询以配合它

时间:2012-03-18 01:26:59

标签: mysql hibernate database-design hibernate-mapping

我在获取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>

1 个答案:

答案 0 :(得分:2)

您的查询使用引用的语法(ManyToOne),但是一个/多对多需要显式连接才能拥有元素的别名

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