Java 使用hibernate进行计数时,哪个解决方案的性能更好
我有两个实体。一个是Product,另一个是ProductCategory。Product-to-ProductCategory关系是多对一。我有一个方法来获取所有ProductCategory。我想向ProductCategory添加一个临时变量productCount,其中应包含每个ProductCategory的可用产品数量。我有两个解决方案。这两个都是工作很好 解决方案1Java 使用hibernate进行计数时,哪个解决方案的性能更好,java,hibernate,Java,Hibernate,我有两个实体。一个是Product,另一个是ProductCategory。Product-to-ProductCategory关系是多对一。我有一个方法来获取所有ProductCategory。我想向ProductCategory添加一个临时变量productCount,其中应包含每个ProductCategory的可用产品数量。我有两个解决方案。这两个都是工作很好 解决方案1 public List<ProductCategory> getProductCategoryList(
public List<ProductCategory> getProductCategoryList() {
List<ProductCategory> productCategoryList = this
.getCurrentSession()
.createQuery(
"SELECT pc FROM ProductCategory pc")
.list();
for (ProductCategory category : productCategoryList) {
String categoryId = category.getId().toString();
category.setProductsCount(getCurrentSession()
.createQuery(
"SELECT p FROM Product p WHERE p.productCategory.id=:categoryId")
.setParameter("categoryId", Long.parseLong(categoryId))
.list().size());
}
return productCategoryList;
}
public列表getProductCategoryList(){
List productCategoryList=此
.getCurrentSession()
.createQuery(
“从ProductCategory pc中选择pc”)
.list();
for(ProductCategory类别:productCategoryList){
String categoryId=category.getId().toString();
category.setProductsCount(getCurrentSession())
.createQuery(
“从产品p中选择p,其中p.productCategory.id=:categoryId”)
.setParameter(“categoryId”,Long.parseLong(categoryId))
.list().size());
}
返回productCategoryList;
}
解决方案2
public List<ProductCategory> getProductCategoryList() {
List<ProductCategory> productCategoryList = new ArrayList<ProductCategory>();
List<Object[]> resultList = this.getCurrentSession()
.createQuery("FROM Product p right join p.productCategory")
.list();
for (Object[] array : resultList) {
Product product = null;
ProductCategory productCategory = null;
if (array[1] != null) {
productCategory = (ProductCategory) array[1];
}
if (array[0] != null) {
product = (Product) array[0];
productCategory.setProductsCount(productCategory
.getProductsCount() == null ? 1 : productCategory
.getProductsCount() + 1);
}
if (productCategory != null
&& !productCategoryList.contains(productCategory)) {
productCategoryList.add(productCategory);
}
}
return productCategoryList;
}
public列表getProductCategoryList(){
List productCategoryList=新建ArrayList();
List resultList=this.getCurrentSession()
.createQuery(“从产品p右键加入p.productCategory”)
.list();
对于(对象[]数组:结果列表){
Product=null;
ProductCategory ProductCategory=null;
if(数组[1]!=null){
productCategory=(productCategory)数组[1];
}
if(数组[0]!=null){
产品=(产品)数组[0];
productCategory.setProductsCount(productCategory
.getProductsCount()==null?1:productCategory
.getProductsCount()+1);
}
如果(productCategory!=null
&&!productCategoryList.contains(productCategory)){
productCategoryList.add(productCategory);
}
}
返回productCategoryList;
}
从这两个方面来看,更好的解决方案是什么?还是其他更好的解决方案?
我对hibernate中的性能比较没有很好的了解。如果你总是想得到这个“属性”,那么我认为你可以在POJO中使用
@公式,让它更加透明
类似于ProductCategory.java上的内容(需要测试):
其中id是ProductCategory.java
上的id字段
编辑
顺便说一下,现在不要担心性能。把代码弄清楚就行了。。一旦您让它运行并工作,您可以分析系统并查看它需要调整的位置。这两种解决方案都很复杂,并从数据库中加载所有类别的产品。如果您已经准备好这样做,为什么不简单地将关联设置为双向的,并简单地调用category.getProducts().size()
来获取您的计数
如果确实不想这样做,则只需执行一个查询以获取类别,然后执行一个附加查询以从数据库获取其产品计数:
select category.id, count(product.id) from Product product
inner join product.category category
where category.id in :categoryIds
group by category.id
这将返回一个列表
,其中每个对象[]
包含一个类别ID和相关的产品计数 编写一个测试应用程序并测量两个实现运行的时间。我猜第二个会更快,因为它只进行一次数据库调用。。。结果列表可能有多大?最好使用解决方案一和选择计数(p),而不是制作列表和计数元素。如果您只对计数感兴趣,为什么要获取所有产品。为什么不使用计数查询?一般来说,一个产品类别应该有许多产品,因此将它们全部提取到内存中没有多大意义。使用第一种方法,只需获取计数。@user714965感谢您的评论,这是真的,但我想了解如何更好地实现技术方面的结果。并找到最佳方法。不仅仅是时间方面的性能度量
select category.id, count(product.id) from Product product
inner join product.category category
where category.id in :categoryIds
group by category.id