使用 queryDSL 查找最后一项,使用 LockMode 实现 spring-data-jpa
Find Last item with queryDSL and spring-data-jpa implementation with LockMode
我想使用 Spring-Data-JPA(Hibernate 实现)和 QueryDSL(安全类型)从 table "product" 中找到最后插入的项目以生成 SQL: SELECT * FROM product ORDER BY id DESC LIMIT 1
我已使用@Transactional 从服务中调用 findLast() 并希望设置 LockMode(LockModeType.PESSIMISTIC_WRITE) 并在此 SQL 查询中添加 "SELECT .. FOR UPDATE"。
public class ProductRepositoryImpl extends QueryDslRepositorySupport implements ProductRepositoryCustom {
public ProductRepositoryImpl() {
super(Product.class);
}
//SELECT * FROM PRODUCTS ORDER BY ID DESC LIMIT 1
@Override
public Product findLast() {
QProduct product = QProduct.product;
return from(product).orderBy(product.id.desc()).limit(1L).fetchOne();
}
}
这是生成的SQL
Hibernate:select * 来自 ( select product0_.id 作为 id11,product0_.description 作为描述 21,product0_.price 作为 price31,product0_.productStatus 作为productStatus41, product0_.quantity as quantity51 from PRODUCTS product0 order by product0.id desc ) where rownum <= ?
问题:在存储库扩展 org.springframework.data.jpa.repository.support.QueryDslRepositorySupport 中添加 setLockMode() 的最佳方法是什么?
我已经尝试过了,但是它 return 对象并且需要投射(产品):
getQuerydsl().createQuery(product)
.setLockMode(LockModeType.PESSIMISTIC_WRITE).orderBy(product.id.desc()).limit(1L).fetchOne();
尝试使用这部分代码,我认为它会像这样工作:
QProduct product = QProduct.product;
return (Product)getQuerydsl().createQuery(product)
.setLockMode(LockModeType.PESSIMISTIC_WRITE).orderBy(product.id.desc())
.limit(1L).fetchOne();
Hibernate 将生成以下查询:
休眠:select * from ( select product0_.id as id1_1_, ...... from PRODUCTS product0_ order by product0_.id desc ) where rownum <= ?
Hibernate:select product0_.id as id1_1_, .... from PRODUCTS product0_ where product0_.id=?更新
注意:有两个SQL查询,只能通过ID锁定行。
问题是结构性的,最好分离查询而不是让 Hibernate 这样做。
我想使用 Spring-Data-JPA(Hibernate 实现)和 QueryDSL(安全类型)从 table "product" 中找到最后插入的项目以生成 SQL: SELECT * FROM product ORDER BY id DESC LIMIT 1
我已使用@Transactional 从服务中调用 findLast() 并希望设置 LockMode(LockModeType.PESSIMISTIC_WRITE) 并在此 SQL 查询中添加 "SELECT .. FOR UPDATE"。
public class ProductRepositoryImpl extends QueryDslRepositorySupport implements ProductRepositoryCustom {
public ProductRepositoryImpl() {
super(Product.class);
}
//SELECT * FROM PRODUCTS ORDER BY ID DESC LIMIT 1
@Override
public Product findLast() {
QProduct product = QProduct.product;
return from(product).orderBy(product.id.desc()).limit(1L).fetchOne();
}
}
这是生成的SQL
Hibernate:select * 来自 ( select product0_.id 作为 id11,product0_.description 作为描述 21,product0_.price 作为 price31,product0_.productStatus 作为productStatus41, product0_.quantity as quantity51 from PRODUCTS product0 order by product0.id desc ) where rownum <= ?
问题:在存储库扩展 org.springframework.data.jpa.repository.support.QueryDslRepositorySupport 中添加 setLockMode() 的最佳方法是什么?
我已经尝试过了,但是它 return 对象并且需要投射(产品):
getQuerydsl().createQuery(product)
.setLockMode(LockModeType.PESSIMISTIC_WRITE).orderBy(product.id.desc()).limit(1L).fetchOne();
尝试使用这部分代码,我认为它会像这样工作:
QProduct product = QProduct.product;
return (Product)getQuerydsl().createQuery(product)
.setLockMode(LockModeType.PESSIMISTIC_WRITE).orderBy(product.id.desc())
.limit(1L).fetchOne();
Hibernate 将生成以下查询: 休眠:select * from ( select product0_.id as id1_1_, ...... from PRODUCTS product0_ order by product0_.id desc ) where rownum <= ?
Hibernate:select product0_.id as id1_1_, .... from PRODUCTS product0_ where product0_.id=?更新
注意:有两个SQL查询,只能通过ID锁定行。 问题是结构性的,最好分离查询而不是让 Hibernate 这样做。