CriteriaBuilder EmbeddedId加入

时间:2013-11-18 18:52:25

标签: java jpa join

我还是Java的新手,但是当我关闭Solr时,我遇到了应用程序框架的问题(要查看对数据库的更改)我收到错误,因为其中一个类仍在使用不使用属性存在的时间越长。

第1部分:我不知道为什么会改变这种情况,并且不再使用ManyToMany,我猜这很可能是一种解决方法。

public class CategoryImpl implements Category, Status, AdminMainEntity {

    @OneToMany(targetEntity = CategoryProductXrefImpl.class, mappedBy = "categoryProductXref.category")
    protected List<CategoryProductXref> allProductXrefs = new ArrayList<CategoryProductXref>(10);

第2部分:

public class CategoryProductXrefImpl implements CategoryProductXref {

    @EmbeddedId
    CategoryProductXrefPK categoryProductXref = new CategoryProductXrefPK();

    public CategoryProductXrefPK getCategoryProductXref() {
        return categoryProductXref;
    }

    public void setCategoryProductXref(CategoryProductXrefPK categoryProductXref) {
        this.categoryProductXref = categoryProductXref;
    }

    public void setCategoryProductXref(CategoryProductXrefPK categoryProductXref) {
        this.categoryProductXref = categoryProductXref;
    }

    public Category getCategory() {
        return categoryProductXref.getCategory();
    }


    public void setCategory(Category category) {
        categoryProductXref.setCategory(category);
    }


    public Product getProduct() {
        return categoryProductXref.getProduct();
    }

    public void setProduct(Product product) {
        categoryProductXref.setProduct(product);
    }

第3部分:

@Embeddable
public class CategoryProductXrefPK implements Serializable {

    @ManyToOne(targetEntity = CategoryImpl.class, optional=false)
    @JoinColumn(name = "CATEGORY_ID")
    protected Category category = new CategoryImpl();

    @ManyToOne(targetEntity = ProductImpl.class, optional=false)
    @JoinColumn(name = "PRODUCT_ID")
    protected Product product = new ProductImpl();

问题在于ProductDao

public List<Product> readFilteredActiveProductsByCategory(Long categoryId, Date currentDate, 
        ProductSearchCriteria searchCriteria) {
    // Set up the criteria query that specifies we want to return Products
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Product> criteria = builder.createQuery(Product.class);

    // The root of our search is Category since we are browsing
    Root<CategoryImpl> category = criteria.from(CategoryImpl.class);

    // We want to filter on attributes from product and sku
    Join<Category, Product> product = category.join("allProducts"); <--Problem Occurs Here
    Join<Product, Sku> sku = product.join("defaultSku");

    // Product objects are what we want back
    criteria.select(product);

我试过这个:

    Join<CategoryImpl, CategoryProductXrefImpl> catXref = category.join("allProductXrefs");
    Join<CategoryProductXrefImpl, Product> product = catXref.join("product");
    Join<Product, Sku> sku = product.join("defaultSku");

我已经通过实施尝试了它,尝试使用第三个连接:

Join<CategoryProductXrefImpl, CategoryProductXrefPK>, 

尝试使用CategoryProductXrefPK而不是XrefImpl,尝试使用CategoryProductXref作为第二个Root。我花了几天时间进行搜索和尝试不同的事情。我没有想法了。

0 个答案:

没有答案