Hibernate 连接查询条件并获取包含两列的连接表

发布于 2024-11-26 05:36:42 字数 1498 浏览 2 评论 0原文

我有一个表cartitems,其中存储了添加到购物车的商品。它有以下列:

其他几列

id(主键)、sku、数量、userId、状态、尺寸以及我还有的 productAvailability 表,我们在其中存储库存水平,并且其中有以下列:

sku、尺寸、数量、BatchId(主键)

product 表还具有与 sku 键上的 productAvailability 表和 cartitems 表的一对多关系

现在,对于状态 = 1 且 userId= 的每个 cartitems x 我们需要检查产品的可用性,如果可用,我需要 CartItems 行和产品的 BatchId:

我到达这里:

 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();

上面基本上执行以下操作:

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

这样我无法获取 BatchId。我尝试使用 JOIN FETCHTYPE 但它仍然加载包含所有大小的 sku 的 ProductAvailability 表。有什么方法可以获取每个 cartItem 的 cartItem 和相应的 BatchId 吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

π浅易 2024-12-03 05:36:42

在您的示例中,您仅从父类中选择项目,因此您无权访问子关联数据是有道理的。

您可以使用 Projections 选择您想要(或确实需要)的字段,然后使用 ResultTransformer 加载 JavaBean。 BatchId,然后将以下内容添加到您的 Criteria 查询中:

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));

对于您的情况,我将使用一个字段来扩展您的 CartItems 类来保存您可能需要向投影字段添加别名,以便转换正常工作。

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.

彼岸花ソ最美的依靠 2024-12-03 05:36:42

因为 CartItem 表和 ProductAvailability 表之间存在一对多关系。您的购物车商品模型中将包含一套、清单或袋子。在 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" />

现在..在抛出上述标准之后...您实际上可以..使用 getProductAvailability 并迭代并获取batchid。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文