Question

I have a table cartitems where the items added to the cart are stored in. It has columns:

id (Primary Key), sku, quantity, userId, status, size and couple of other columns

I also have a productAvailability table in which we store Inventory levels and where I have following columns:

sku, size , quantity, BatchId (Primary Key)

Also product table has one-to-many relationships with productAvailability table and cartitems table both on the sku key

Now, for each cartitems with status = 1 and userId=x we need to check the availability of the product and if available, I need the CartItems row and the BatchId of the product:

I got till here:

 Criteria criteria = session.createCriteria(CartItems.class,"cartitems");
            criteria.add(Restrictions.eq("userId", userId));
            criteria.add(Restrictions.eq("status", status));
            criteria.createAlias("product", "product");
            criteria.createAlias("product.productAvailability", "productavailability");
            criteria.add(Restrictions.eqProperty("productavailability.size", "cartitems.size"));
            criteria.add(Restrictions.geProperty("productavailability.quantity", "cartitems.quantity"));
                    List<CartItems> cartItems =  criteria.list();

The above basically does following:

select * from cartItems 
    where productAvailability.quantity >= cartitems.quantity and
          productAvailability.size = cartitems.size and
          productAvailability.sku = cartitems.sku and
          cartitems.userId = ? and
          cartitems.status = ?

This way I cannot get the BatchId. I tried using JOIN FETCHTYPE but it still loads productAvailability table with all sizes of an sku. Is there any method to get both cartItems and corresponding BatchIds for each of the cartItems?

Was it helpful?

Solution

In your example, you are only selecting items from the parent class, so it makes sense that you don't have access to child association data.

You could use Projections to select those fields you want (or really, need), and then use a ResultTransformer to load up a JavaBean. In your case, I would extend your CartItems class with a field to hold the BatchId, and then add the following to your Criteria query:

ProjectionList projections = Projections.projectionList(); 
projections.add(Projections.property("sku"));
projections.add(Projections.property("quantity"));
...etc...
projections.add(Projections.property("productavailability.BatchId", "BatchId"));
criteria.setProjection(projections);
criteria.setResultTransformer(Transformers.aliasToBean(CartItemsTO.class));

Note that you might need to add aliases to the Projection fields so that transformation works properly.

OTHER TIPS

Because you have a one to many relationship between the CartItem Table and the productAvailability table. You would be having a set, or list or bag in your Cart Item Model. and the mapping would be something like this in hbm.

<set name="productAvailabilitySet" table="table_product_availability"
        inverse="true" fetch="select" cascade="save-update" lazy="false">
        <key column="table_availability2cart_item" />
        <one-to-many
    class="com.example.ProductAvailability" />

Now.. after throwing above Criteria... you could actually.. use getProductAvailability and iterate and get the batchid's.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top