I have two domain objects like below
User.java
Public class User{
private int userId;
private String emailId;
private Product product;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PRODUCT_ID")
public Product getProduct() {
return this.product;
}
}
Product.java
public class Product{
@Id
@GeneratedValue
@Column(name = "PRODUCT_ID", unique = true, nullable = false)
private productid;
}
In this scenario User to Product has Many-to-One relation.A multiple user can subscribe to same product.I wanted to query for user Email Ids in the User table with productId which is primary key in the Product table and foriegn key in the User table by joining those two tables..How can do that using hibernate query language or Criteria by joining those two tables?
In hql you dont need to define the join columns like you do in sql by using the “on” keyword.
You already defined the join keys in the mapping , so hibernate will do the join based on them.
So in your case
Select u.emailid from user u where u.product.productid = your product id.
For more read on hql and you also should read about named queries