标准和预测

发布于 2024-11-25 00:07:08 字数 990 浏览 1 评论 0原文

我有一个实体:订单详细信息。它看起来如下:

@ManyToOne
public Product getProduct() {
    return this.product;
}
public void setProduct(Product product) {
    this.product = product;
}

@Column(name="amount",nullable = false)
public int getAmount() {
    return this.amount;
}
public void setAmount(int amount) {
    this.amount= amount;
}

@Transient
public void setSum_amount(double sum_amount){
    this.sum_amount=sum_amount;
}

我试图获取订单详细信息列表、按产品分组以及订单详细信息表中每个产品的数量。

这是我执行此操作的代码:

    session=sessionFactory.openSession();
    List<OrderDetail> od=session.createCriteria(OrderDetail.class) 
    .setProjection( Projections.projectionList()
            .add( Projections.sum("amount"),"sum_amount")
            .add( Projections.groupProperty("product"))
    )
    .setResultTransformer(Transformers.aliasToBean(OrderDetail.class))
    .list();

问题是我无法访问产品属性,执行“od.getProduct().getName()”我得到 nullpointerException,那么,我该如何修复它?

I have an entity: Order Detail. It look like as follows:

@ManyToOne
public Product getProduct() {
    return this.product;
}
public void setProduct(Product product) {
    this.product = product;
}

@Column(name="amount",nullable = false)
public int getAmount() {
    return this.amount;
}
public void setAmount(int amount) {
    this.amount= amount;
}

@Transient
public void setSum_amount(double sum_amount){
    this.sum_amount=sum_amount;
}

I'm trying to get a list of order details, group by products, and the amount of each product in Order Detail table.

This is my code to do that:

    session=sessionFactory.openSession();
    List<OrderDetail> od=session.createCriteria(OrderDetail.class) 
    .setProjection( Projections.projectionList()
            .add( Projections.sum("amount"),"sum_amount")
            .add( Projections.groupProperty("product"))
    )
    .setResultTransformer(Transformers.aliasToBean(OrderDetail.class))
    .list();

The issue is that I can not access to the product property, doing "od.getProduct().getName()" i get the nullpointerexception, so, how could i fix it?

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

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

发布评论

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

评论(2

最美的太阳 2024-12-02 00:07:08

您实际上获得的并不是订单详细信息列表,而是每个订单详细信息组的总和列表。因此,您可以从 SQL 获取数值。因此,我认为,您将获得空 OrderDetail (由于转换器)对象的列表,其中仅设置了 sum_amount

您的问题无法以其当前形式得到解决,因为您的查询没有获取订单详细信息,而是获取完全不同的对象。您应该重新考虑您想要实现的目标以及如何实现。

You are not really getting a list of order details but list of sums for each order detail group. So, you are getting numeric values from SQL. Therefore, I think, you are getting list of empty OrderDetail (because of transformer) objects with only sum_amount set on them.

Your problem can't be solved in its current form as your query is not getting order details but completely different objects. You should rethink what you are trying to achieve and how.

兰花执着 2024-12-02 00:07:08

当您使用Projections对事物进行分组时,您根据定义限制了查询返回的内容。在上面的示例中,您仅返回 2 列,因此 @ManyToOne 关联未初始化。获得结果集后,尝试循环它,对于每条记录,使用 Hibernate.initialize 加载关联。

When you use Projections to group things, you are limiting by definition what comes back from your query. In your example above, you are only returning 2 columns, so the @ManyToOne association isn't being initialized. Once you have the result set, try looping it, and for each record, use Hibernate.initialize to load up the association.

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