hibernate join field's column condition by using criteria
I have two entities related and I want to query like below:
SELECT r from Recipe as r join r.category as cate where cate.categoryId = 3;
The above code is HQL. And I want it to be translated into Criteria Code.
I want to set condition on join field but I can not find how to do that. When I google it, it just tells me about setting condition for main table, not the condition for field entitiy's property.
Here is my entities:
package com.musicovery12.cookingstep.persistence.model;
// omitted
@Entity
@Table(name="recipe")
public class Recipe {
// ... omitted
private FoodCategory2 category;
@ManyToOne(optional=false)
@JoinColumn(name="category#", referencedColumnName="category#", nullable=false)
public FoodCategory2 getCategory() {
return category;
}
public void setCategory(FoodCategory2 category) {
this.category = category;
}
}
and here's join field entity.
package com.musicovery12.cookingstep.persistence.model;
@Entity
@Table(name="food_category2", uniqueConstraints={@UniqueConstraint(columnNames="category_nm")})
public class FoodCategory2 {
private int categoryId;
// ... omitted
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_gen")
@SequenceGenerator(name="seq_gen", sequenceName="seq_food_cate", allocationSize=1)
@Column(name="category#", unique=true, nullable=false)
public int getCategoryId() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}
}
链接地址: http://www.djcxy.com/p/37070.html