I've some problems with an HQL query:
@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);
This query returns all categories (seems it ignore the second AND clause) but only the sport that matchs with sportId.
These are my classes that represents Sport and Category:
@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
And
@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
EDIT
Suppose that:
Sport:
|sportId | SportName
|1 |Rugby
|2 |Basket
|3 |Tennis
And Categories:
|categoryId | CategoryName | sportId|
|1 |Italy |1 |
|2 |England |2 |
|3 |USA |1 |
|1 |Spain |2 |
I want to obtain with my query (suppose sportId=1 and categoryId=1):
| sportId | SportName | categoryId | CategoryName |
1 Rugby 1 Italy
Someone can help me? Thanks in advance
That won't work. Look at this .
You can invert the query to get the categories that have some specific sport:
Select c from Categories c JOIN Sport s where c.categoryId = :categoryId and s.sportId = :sportId
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.