![](/img/trans.png)
[英]Hibernate; HQL; why does the delete query not work, but select does?
[英]HQL query does not work properly
HQL查詢存在一些問題:
@Query("Select s from Sport s JOIN s.categories as c where s.sportId = :sportId and c.categoryId = :categoryId")
Iterable<Sport> findByCategoryIdAndSportId(@Param("sportId")Long sportId, @Param("categoryId")Long categoryId);
該查詢返回所有類別(似乎忽略了第二個AND子句),但僅返回與sportId匹配的運動。
這些是我代表運動和類別的課程:
@Entity
@Table(name = "Sports")
public class Sport implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long sportId;
@OneToMany(mappedBy = "sportId")
@ApiModelProperty(required = true)
@JsonManagedReference
private Set<Category> categories;
@Column
private String sportName;
....get and set
和
@Entity
@Table(name = "Categories")
public class Category implements Serializable
{
@ManyToOne
@JoinColumn(name = "sportId", nullable = false)
@JsonBackReference
private Sport sportId;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long categoryId;
..get and set
編輯
假設:
Sport:
|sportId | SportName
|1 |Rugby
|2 |Basket
|3 |Tennis
和類別:
|categoryId | CategoryName | sportId|
|1 |Italy |1 |
|2 |England |2 |
|3 |USA |1 |
|1 |Spain |2 |
我想通過查詢獲得(假設sportId = 1和categoryId = 1):
| sportId | SportName | categoryId | CategoryName |
1 Rugby 1 Italy
有人可以幫我嗎? 提前致謝
那行不通。 看這個 。
您可以反轉查詢以獲取具有某些特定運動的類別:
Select c from Categories c JOIN Sport s where c.categoryId = :categoryId and s.sportId = :sportId
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.