我是JPA的新手.所以我的问题对某些人来说应该是如此简单.
下面是SQL中的简单查询,我想将其转换为JPA.我已经有一个实体类叫TimeEnt.
SELECT
SUM(TimeEntryActualHours) as UnBilledHrs,
SUM (TimeEntryAmount) as UnbilledAmount
FROM TimeEnt WHERE MatterID = 200
Run Code Online (Sandbox Code Playgroud) 我正在使用Spring数据jpa 1.2,我无论如何都找不到像下面那样检索聚合查询结果.
select count(v), date(v.createTimestamp) from UserEntity v
group by date(v.createTimestamp)
Run Code Online (Sandbox Code Playgroud)
哪个与原生JPA完美配合
@Entity()
public class UserEntity {
@Id
private long id;
.
.
@Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
private Timestamp createTimestamp;
}
Run Code Online (Sandbox Code Playgroud)
我的JPA存储库是
public interface UserRepository extends JpaRepository<UserEntity, Long>,
JpaSpecificationExecutor<UserEntity> {
}
Run Code Online (Sandbox Code Playgroud)
那么如何才能进行聚合查询抛出Spring数据,我在文档中找不到任何内容
我使用spring数据和JpaSpecificationExecutor::findAll方法来获取模型。调用此方法时如何使用查询提示?
上面的源代码工作正常,但是我无法为我的JPA提供程序(在我的情况下为EclipseLink)设置QueryHint。
@Repository
public interface ProductRepository extends JpaRepository<Product, Integer>, JpaSpecificationExecutor<Product> {
}
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
public List<Product> findByTitle(String locale, String titleToSearch) {
return productRepository.findAll((Root<ProductCategory> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> {
return builder.equal(builder.function("jsonb_extract_path_text", String.class, root.<String>get("title"), builder.literal(locale)), titleToSearch);
});
}
}
Run Code Online (Sandbox Code Playgroud)
以上是我使用spring-data使用查询提示的方式,
@Repository
public interface ProductRepository extends JpaRepository<Product, Integer>, JpaSpecificationExecutor<Product> {
@QueryHints(value = {
@QueryHint(name = org.eclipse.persistence.config.QueryHints.BATCH_TYPE, value = "JOIN"),
@QueryHint(name = org.eclipse.persistence.config.QueryHints.BATCH, value = "p.productCategory"),
@QueryHint(name = org.eclipse.persistence.config.QueryHints.BATCH, value = "p.productFileList")
}, …Run Code Online (Sandbox Code Playgroud)