Skip to content

Commit 4cadc55

Browse files
Merge pull request shopizer-ecommerce#386 from wtune/add_getProductWithOnlyMerchantStoreById
Add a dedicated query to reduce the amount of unnecessary joined table for product
2 parents 29c4933 + f4118c6 commit 4cadc55

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

sm-core/src/main/java/com/salesmanager/core/business/repositories/catalog/product/ProductRepositoryCustom.java

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Product getProductForLocale(long productId, Language language,
3535
Locale locale);
3636

3737
Product getById(Long productId);
38+
Product getProductWithOnlyMerchantStoreById(Long productId);
3839
Product getById(Long productId, MerchantStore merchant);
3940

4041
Product getByCode(String productCode, Language language);

sm-core/src/main/java/com/salesmanager/core/business/repositories/catalog/product/ProductRepositoryImpl.java

+19-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
import java.util.List;
66
import java.util.Locale;
77
import java.util.Set;
8-
import javax.persistence.EntityManager;
9-
import javax.persistence.NonUniqueResultException;
10-
import javax.persistence.PersistenceContext;
11-
import javax.persistence.Query;
8+
import javax.persistence.*;
9+
1210
import org.apache.commons.collections4.CollectionUtils;
1311
import org.apache.commons.lang3.StringUtils;
1412
import org.slf4j.Logger;
@@ -40,7 +38,23 @@ public Product getById(Long productId) {
4038
return this.get(productId, null);
4139
}
4240

43-
private Product get(Long productId, MerchantStore merchant) {
41+
@Override
42+
public Product getProductWithOnlyMerchantStoreById(Long productId) {
43+
final String hql = "select distinct p from Product as p " +
44+
"join fetch p.merchantStore merch " +
45+
"where p.id=:pid";
46+
47+
final Query q = this.em.createQuery(hql);
48+
q.setParameter("pid", productId);
49+
50+
try {
51+
return (Product)q.getSingleResult();
52+
} catch (NoResultException ignored) {
53+
return null;
54+
}
55+
}
56+
57+
private Product get(Long productId, MerchantStore merchant) {
4458

4559
try {
4660

sm-core/src/main/java/com/salesmanager/core/business/services/catalog/product/ProductService.java

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ List<Product> getProducts(List<Long> categoryIds, Language language)
4040

4141
Product getBySeUrl(MerchantStore store, String seUrl, Locale locale);
4242

43+
Product getProductWithOnlyMerchantStoreById(Long productId);
4344
/**
4445
* Get a product by sku (code) field and the language
4546
* @param productCode

sm-core/src/main/java/com/salesmanager/core/business/services/catalog/product/ProductServiceImpl.java

+5
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ public List<Product> getProducts(List<Long> categoryIds) throws ServiceException
113113
public Product getById(Long productId) {
114114
return productRepository.getById(productId);
115115
}
116+
117+
@Override
118+
public Product getProductWithOnlyMerchantStoreById(Long productId) {
119+
return productRepository.getProductWithOnlyMerchantStoreById(productId);
120+
}
116121

117122
@Override
118123
public List<Product> getProducts(List<Long> categoryIds, Language language) throws ServiceException {

sm-shop/src/main/java/com/salesmanager/shop/admin/controller/products/ProductReviewController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public String displayProductReviews(@RequestParam("id") long productId,Model mod
104104

105105
try {
106106

107-
product = productService.getById(productId);
107+
product = productService.getProductWithOnlyMerchantStoreById(productId);
108108

109109

110110
if(product==null) {

0 commit comments

Comments
 (0)