简体   繁体   中英

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. 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. 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...

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?

Also, check this link for the syntax: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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