为什么Spring JPA / Hibernate更新时返回0?

时间:2018-09-02 18:48:25

标签: spring hibernate jpa spring-data-jpa

我有此查询,即使它更新一行,它也会返回0而不是1。 这是我的代码:

@Modifying
    @Query("UPDATE Tshirt SET smallSizeInStock = smallSizeInStock - :quantityBoughtByUser WHERE id = :productId and (smallSizeInStock - :quantityBoughtByUser) >= 0")
    int updateSmallQuantityInStock(@Param("quantityBoughtByUser") int quantityBoughtByUser, @Param("productId")Long productId);

这就是我所说的:

   int rowsUpdated = tshirtProductRepository.updateSmallQuantityInStock(productCart.getQuantity().intValue(), productId);

然后,如果没有行被更新,我将具有以下代码:

if (rowsUpdated == 0) {
            throw new AppException("The tshirt quantity you tried to buy for the product "
                    + productCart.getProductDTO().getName() + " is not available.");
        }

问题是它返回0,即使我可以看到该行正在更新。而且我看到它反映在数据库中。如果删除if语句,一切正常,但是在此步骤中没有行更新时,我需要回滚。

这是我在更新时在控制台上看到的:

2018-09-02 11:43:13.571 DEBUG 23328 --- [nio-5000-exec-8] org.hibernate.SQL                        : update products set updated_at=?, category_id=?, deleted_at=?, description=?, in_stock=?, name=?, notify_low_stock=?, promotion_price=?, quantity_in_stock=?, quantity_sold=?, regular_price=?, short_description=?, sku=?, store_id=?, weight=? where id=?
2018-09-02 11:43:13.571 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [TIMESTAMP] - [Sun Sep 02 18:43:13 UTC 2018]
2018-09-02 11:43:13.571 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [BIGINT] - [1]
2018-09-02 11:43:13.571 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [TIMESTAMP] - [null]
2018-09-02 11:43:13.571 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [VARCHAR] - [This is my big description]
2018-09-02 11:43:13.571 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [5] as [BOOLEAN] - [false]
2018-09-02 11:43:13.571 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [6] as [VARCHAR] - [My New Product]
2018-09-02 11:43:13.571 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [7] as [BOOLEAN] - [true]
2018-09-02 11:43:13.571 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [8] as [DOUBLE] - [799.99]
2018-09-02 11:43:13.571 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [9] as [INTEGER] - [null]
2018-09-02 11:43:13.572 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [10] as [INTEGER] - [0]
2018-09-02 11:43:13.572 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [11] as [DOUBLE] - [899.99]
2018-09-02 11:43:13.572 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [12] as [VARCHAR] - [This is my short description]
2018-09-02 11:43:13.572 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [13] as [VARCHAR] - [ABC-DEF]
2018-09-02 11:43:13.572 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [14] as [BIGINT] - [1]
2018-09-02 11:43:13.572 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [15] as [DOUBLE] - [50.0]
2018-09-02 11:43:13.572 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [16] as [BIGINT] - [1]
2018-09-02 11:43:13.573 DEBUG 23328 --- [nio-5000-exec-8] org.hibernate.SQL                        : update tshirts set extra_large_size_in_stock=?, large_size_in_stock=?, medium_size_in_stock=?, small_size_in_stock=?, unique_size_in_stock=? where id=?
2018-09-02 11:43:13.573 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [INTEGER] - [null]
2018-09-02 11:43:13.573 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [INTEGER] - [null]
2018-09-02 11:43:13.573 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [INTEGER] - [null]
2018-09-02 11:43:13.573 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [INTEGER] - [-11]
2018-09-02 11:43:13.573 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [5] as [INTEGER] - [null]
2018-09-02 11:43:13.573 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [6] as [BIGINT] - [1]
2018-09-02 11:43:13.574 DEBUG 23328 --- [nio-5000-exec-8] org.hibernate.SQL                        : create temporary  table HT_tshirts (id int8 not null) 
2018-09-02 11:43:13.575 DEBUG 23328 --- [nio-5000-exec-8] org.hibernate.SQL                        : insert into HT_tshirts select tshirt0_.id as id from tshirts tshirt0_ inner join products tshirt0_1_ on tshirt0_.id=tshirt0_1_.id where tshirt0_.id=? and tshirt0_.small_size_in_stock-?>=0
2018-09-02 11:43:13.575 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [1]
2018-09-02 11:43:13.575 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [INTEGER] - [7]
2018-09-02 11:43:13.576 DEBUG 23328 --- [nio-5000-exec-8] org.hibernate.SQL                        : update tshirts set small_size_in_stock=small_size_in_stock-? where (id) IN (select id from HT_tshirts)
2018-09-02 11:43:13.576 TRACE 23328 --- [nio-5000-exec-8] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [INTEGER] - [7]
2018-09-02 11:43:13.577 DEBUG 23328 --- [nio-5000-exec-8] org.hibernate.SQL                        : drop table HT_tshirts

我的TShirt对象扩展了Product,它们共享相同的ID。

更新: 因此,在调用更新方法之前,我已经调用了一个检索方法。 如果我删除检索,那么更新一次就可以了。我想知道为什么会这样。

0 个答案:

没有答案