简体   繁体   English

Hibernate 从 HQL 生成错误的查询

[英]Hibernate generates the wrong query from HQL

I have an application that tracks performance stats on inventory for products at multiple warehouses.我有一个应用程序可以跟踪多个仓库中产品库存的性能统计数据。 This is used for purchasing planning and other business activities.这用于采购计划和其他业务活动。 This information is queried far more often than it changes, and it's expensive to calculate, so I've chosen to add a cache table to the database.查询此信息的次数远多于更改的次数,而且计算成本很高,因此我选择向数据库添加缓存表。

My HQL should be selecting the cached statistics entity that belongs to the product and the warehouse, but if you look at the actual SQL query it generates, it's ONLY conditioned in the WHERE clause is on the warehouse.我的 HQL 应该选择属于产品和仓库的缓存统计实体,但是如果您查看它生成的实际 SQL 查询,它仅以WHERE子句位于仓库中为条件。 where inventorys0_.warehouse_id=?

I'm inclined to believe this is a bug in Hibernate, but I'd like feedback that I'm doing this correctly or not.我倾向于相信这是 Hibernate 中的一个错误,但我希望得到反馈,表明我是否正确地执行了此操作。 I can't find a bug report for this issue in Hibernate's Jira, but I'm not entirely sure what to call the issue in order to know how to search for it...我在 Hibernate 的 Jira 中找不到此问题的错误报告,但我不完全确定该问题的名称是什么,以便知道如何搜索它...

public Optional<InventoryStatsCacheEntity> findByWarehouse(@Nonnull WarehouseEntity warehouse, @Nonnull SimpleProductEntity simpleProduct) {
    return findOne(entityManager()
        .createQuery("select i from InventoryStatsCacheEntity i where i.warehouse=:warehouse and i.simpleProduct=:simpleProduct", InventoryStatsCacheEntity.class)
        .setParameter("warehouse", warehouse)
        .setParameter("simpleProduct", simpleProduct));
}
select inventorys0_.warehouse_id as warehous8_15_0_, inventorys0_.id as id1_15_0_, inventorys0_.id as id1_15_1_, inventorys0_.cls as cls2_15_1_, inventorys0_.version as version3_15_1_, inventorys0_.simple_product_id as simple_p7_15_1_, inventorys0_.timestamp as timestam4_15_1_, inventorys0_.warehouse_id as warehous8_15_1_, inventorys0_.weekly_consumption_rate as weekly_c5_15_1_, inventorys0_.weekly_consumption_variance as weekly_c6_15_1_ from inventory_stats inventorys0_ where inventorys0_.warehouse_id=?

Because you asked... ;)因为你问...;)

@Entity
@Table(name = InventoryStatsCacheEntity.TABLE, uniqueConstraints = {
        @UniqueConstraint(name = InventoryStatsCacheEntity.CONSTRAINT_UNIQUE_PRODUCT_WAREHOUSE_ID, columnNames = {"simple_product_id", "warehouse_id"}),
})
public class InventoryStatsCacheEntity extends GraphQLEntity {

    public static final String TABLE = "inventory_stats";

    public static final String CONSTRAINT_UNIQUE_PRODUCT_WAREHOUSE_ID = "unique_product_warehouse_id";

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    protected SimpleProductEntity simpleProduct;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    protected WarehouseEntity warehouse;

    @NotNull
    @Column(nullable = false)
    private Instant timestamp;

    @NotNull
    @Column(nullable = false)
    private Float weeklyConsumptionRate;

    @NotNull
    @Column(nullable = false)
    private Float weeklyConsumptionVariance;

    /* getters and setters omitted for brevity */
}

What is the structure of InventoryStatsCacheEntity, WarehouseEntity, and SimpleProductEntity? InventoryStatsCacheEntity、WarehouseEntity 和 SimpleProductEntity 的结构是什么?

Also, check this link for the syntax: How to use setParameter in Hibernate另外,请检查此链接的语法: How to use setParameter in Hibernate

I suggest you to test it by using the wrong value, and then see what happens in the log file:我建议您使用错误的值进行测试,然后查看日志文件中会发生什么:

select i from InventoryStatsCacheEntity i where i.warehouse=:warehouse and i.simpleProduct=:wrongvalue

Test it step by step, it should work!一步一步测试它,它应该可以工作! If the log file print errors, then you should be able to find the root cause, if no errors in the log file, it may relate to other thing.如果日志文件打印错误,那么您应该能够找到根本原因,如果日志文件中没有错误,则可能与其他事情有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM