Hello,
I am stuck in creating an inner-join namedquery.
I have a Product table, CategorySubcategories table and a ProductCategoryAssociate table.
My inner join query is shown as below:
Code:
select * from product p
inner join category_subcategories c
where c.category_name = 'Electronic';
In CategorySubcategoryAssociate class, I created the following namedquery:
Code:
@NamedQuery(name = "ProductCategory.findProductJoinByCategoryName", query =
"select p from ProductCategoryAssociate p "
+ "where p.productCategoryAssociatePK.categoryId = p.productCategoryAssociatePK.productId "
+ "and p.categorySubcategories.category_name = :categoryName")
In my facade class:
Code:
public List<Product> findAllProductsByCategoryName(String categoryName) {
Query q = em.createNamedQuery("ProductCategory.findProductJoinByCategoryName");
q.setParameter("categoryName", categoryName);
try {
return (List<Product>) q.getResultList();
} catch (NoResultException e) {
return null;
}
}
However the above coding thrown Unknown error (in Netbeans 7.).
Here is my ProductCategoryAssociate class:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
Code:
package au.com.houseware.server.ejb.entity;
@Entity
@Table(name = "product_category_associate", catalog = "houseware", schema = "")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "ProductCategoryAssociate.findAll", query = "SELECT p FROM ProductCategoryAssociate p"),
@NamedQuery(name = "ProductCategoryAssociate.findByProductId", query = "SELECT p FROM ProductCategoryAssociate p WHERE p.productCategoryAssociatePK.productId = :productId"),
@NamedQuery(name = "ProductCategoryAssociate.findByCategoryId", query = "SELECT p FROM ProductCategoryAssociate p WHERE p.productCategoryAssociatePK.categoryId = :categoryId"),
@NamedQuery(name = "ProductCategoryAssociate.findByTotalItems", query = "SELECT p FROM ProductCategoryAssociate p WHERE p.totalItems = :totalItems"),
@NamedQuery(name = "ProductCategory.findProductJoinByCategoryName", query =
"select p from ProductCategoryAssociate p "
+ "where p.productCategoryAssociatePK.categoryId = p.productCategoryAssociatePK.productId "
+ "and p.categorySubcategories.category_name = :categoryName")
})
public class ProductCategoryAssociate implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected ProductCategoryAssociatePK productCategoryAssociatePK;
@Column(name = "total_items")
private Integer totalItems;
@JoinColumn(name = "category_id", referencedColumnName = "category_id", nullable = false, insertable = false, updatable = false)
@ManyToOne(optional = false)
private CategorySubcategories categorySubcategories;
@JoinColumn(name = "product_id", referencedColumnName = "product_id", nullable = false, insertable = false, updatable = false)
@ManyToOne(optional = false)
private Product product;
public ProductCategoryAssociate() {
}
public ProductCategoryAssociate(ProductCategoryAssociatePK productCategoryAssociatePK) {
this.productCategoryAssociatePK = productCategoryAssociatePK;
}
public ProductCategoryAssociate(int productId, int categoryId) {
this.productCategoryAssociatePK = new ProductCategoryAssociatePK(productId, categoryId);
}
public ProductCategoryAssociatePK getProductCategoryAssociatePK() {
return productCategoryAssociatePK;
}
public void setProductCategoryAssociatePK(ProductCategoryAssociatePK productCategoryAssociatePK) {
this.productCategoryAssociatePK = productCategoryAssociatePK;
}
public Integer getTotalItems() {
return totalItems;
}
public void setTotalItems(Integer totalItems) {
this.totalItems = totalItems;
}
public CategorySubcategories getCategorySubcategories() {
return categorySubcategories;
}
public void setCategorySubcategories(CategorySubcategories categorySubcategories) {
this.categorySubcategories = categorySubcategories;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
@Override
public int hashCode() {
int hash = 0;
hash += (productCategoryAssociatePK != null ? productCategoryAssociatePK.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof ProductCategoryAssociate)) {
return false;
}
ProductCategoryAssociate other = (ProductCategoryAssociate) object;
if ((this.productCategoryAssociatePK == null && other.productCategoryAssociatePK != null) || (this.productCategoryAssociatePK != null && !this.productCategoryAssociatePK.equals(other.productCategoryAssociatePK))) {
return false;
}
return true;
}
@Override
public String toString() {
return "au.com.houseware.server.ejb.entity.ProductCategoryAssociate[ productCategoryAssociatePK=" + productCategoryAssociatePK + " ]";
}
}
What is the correct way to define an Hibernate inner-join namedquery ?
Very appreciate for any suggestion and help.
Thanks
Sam