gpt4 book ai didi

mysql - Hibernate 多对多映射和查询

转载 作者:行者123 更新时间:2023-11-29 08:56:50 24 4
gpt4 key购买 nike

我在掌握 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>

竞争对手

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

最佳答案

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

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

关于mysql - Hibernate 多对多映射和查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9755268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com