-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
java.lang.UnsupportedOperationException: null / the entity via @ElementCollection or a similar collection mapping #1
Comments
Issue Faced with Collection Entities in Hibernate During the development of our project using a Microservices Architecture (MSA), we encountered a challenge while implementing a specific feature involving the direct querying of an entity's collection with Hibernate. The problem arose when attempting to handle the imageUrls field (a collection type) of the Product entity. This field, mapped using @ElementCollection, led to a java.lang.UnsupportedOperationException when directly referenced in a JPA query, indicating Hibernate's lack of support for direct collection references. Approach and Resolution Identifying the Problem: Changing Our Strategy: Implementation Details: Conclusion: @Controller
@RequiredArgsConstructor
public class ProductSearchController {
private final ProductService productService;
@GetMapping("/product/search")
public String productSearchView(@RequestParam(value = "q", required = false) String productName,
@RequestParam(value = "p", required = false) Integer page,
@RequestParam(value = "sortField", required = false) String sortField,
@RequestParam(value = "sortDir", required = false) String sortDir ,
@RequestParam(value = "size", required = false) Integer size,
Model model) {
if (size == null) {
size = 12;
}
if (page==null) {
page = 1;
}
if (sortDir == null) {
sortDir = "desc";
}
if (sortField == null) {
sortField = "productID";
}
if (productName == null) {
return "redirect:/";
}
if (sortField.equals("salesVolume")) {
Page<ProductForSalesVolumeSortDTO> initialProductsPage = productService.findProductsBySalesVolume(productName, PageRequest.of(page - 1, size));
List<ProductForSalesVolumeSortDTO> productPage = productService.completeProductDTOs(initialProductsPage.getContent());
model.addAttribute("productPage", productPage);
model.addAttribute("totalPages", initialProductsPage.getTotalPages());
} else {
Sort sort = Sort.by(Sort.Direction.fromString(sortDir), sortField);
Pageable pageable = PageRequest.of(page-1, size, sort);
Page<Product> productPage = productService.findPageByProductNameContaining(pageable, productName);
model.addAttribute("productPage", productPage);
model.addAttribute("totalPages", productPage.getTotalPages());
}
model.addAttribute("page", page);
model.addAttribute("q", productName);
model.addAttribute("sortField", sortField);
model.addAttribute("sortDir", sortDir);
return "product/productSearchView";
}
} package com.bulkpurchase.domain.dto.product;
import com.bulkpurchase.domain.entity.user.User;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter
@Setter
public class ProductForSalesVolumeSortDTO {
private Long productID;
private String productName;
private Double price;
private Integer stock;
private List<String> imageUrls;
private Long totalQuantity;
private User user;
public ProductForSalesVolumeSortDTO(Long productID, String productName, Double price, Integer stock, List<String> imageUrls, Long totalQuantity, User user) {
this.productID = productID;
this.productName = productName;
this.price = price;
this.stock = stock;
this.imageUrls = imageUrls;
this.totalQuantity = totalQuantity;
this.user = user;
}
public ProductForSalesVolumeSortDTO() {
}
} public Page<ProductForSalesVolumeSortDTO> findProductsBySalesVolume(String productName, Pageable pageable) {
String sql = "SELECT p.PRODUCTID as productID, p.PRODUCT_NAME as productName, p.PRICE as price, p.STOCK as stock, u.USERNAME as username, SUM(od.quantity) as totalQuantity " +
"FROM order_details od " +
"JOIN products p ON od.PRODUCTID = p.PRODUCTID " +
"JOIN users u ON p.USERID = u.USERID " +
"WHERE p.product_name LIKE :productName " +
"GROUP BY p.PRODUCTID, u.USERNAME, p.STOCK, p.PRICE, p.PRODUCT_NAME " +
"ORDER BY totalQuantity DESC";
Query query = entityManager.createNativeQuery(sql);
query.setParameter("productName", "%" + productName + "%");
// 페이징 처리
int totalRows = query.getResultList().size(); // 전체 결과 수
query.setFirstResult((int) pageable.getOffset()); // 페이징 시작점
query.setMaxResults(pageable.getPageSize()); // 페이지 크기
List<Object[]> results = query.getResultList();
List<ProductForSalesVolumeSortDTO> products = new ArrayList<>();
for (Object[] result : results) {
User user = new User();
user.setUsername((String) result[4]);
products.add(new ProductForSalesVolumeSortDTO(
((Number) result[0]).longValue(), // productID
(String) result[1], // productName
((Number) result[2]).doubleValue(), // price
((Number) result[3]).intValue(), // stock
null, // imageUrls 추후 추가
((Number) result[5]).longValue(), // totalQuantity
user // User=
));
}
return new PageImpl<>(products, pageable, totalRows);
}
public List<String> findImageUrlsByProductId(Long productID) {
String sql = "SELECT IMAGE_URL FROM KRKARMA777.PRODUCT_IMAGE_URLS WHERE PRODUCTID = :productID";
Query query = entityManager.createNativeQuery(sql);
query.setParameter("productID", productID);
return query.getResultList();
}
public List<ProductForSalesVolumeSortDTO> completeProductDTOs(List<ProductForSalesVolumeSortDTO> products) {
for (ProductForSalesVolumeSortDTO product : products) {
List<String> imageUrls = findImageUrlsByProductId(product.getProductID());
product.setImageUrls(imageUrls);
}
return products;
} |
I'm encountering an issue with handling collections, such as p.imageUrls, in Hibernate. It appears that while p.imageUrls is included in the entity via @ElementCollection or a similar collection mapping, attempting to directly reference it in a query is not supported by Hibernate, resulting in this problem. Could someone please help me with this?
The text was updated successfully, but these errors were encountered: