使用Hibernate将自定义postgresql映射到Java对象?

时间:2019-02-05 09:47:22

标签: postgresql hibernate hibernate-mapping hibernate-native-query

我一直在寻找不同的解决方案,以了解如何将自定义的postgresql查询(它使用聚合函数和联接)映射到我自己的Java对象列表,但是其中的nnone似乎有效。

这是我的存储库:

@Transactional
public List getShopProductWithTagsList(Integer supplierId) {

    String sqlQuery = "SELECT id AS productId, description, array_agg(tag_id) AS tags " +
            "FROM pood.a_toode AS product LEFT JOIN tags.tag_to_product AS tags ON product.id = tags.product_id " +
            "WHERE tags.deleted IS NULL AND product.keel = 1 AND product.supplierId=" + supplierId +
            "GROUP BY product.id, product.description " +
            "HAVING COUNT(tag_id) > 1";

    NativeQuery query = (NativeQuery) entityManager.createNativeQuery(sqlQuery);
    return (List<ShopProductWithTagsDto>) query.getSingleResult();
}

这是我的dto:

public class ShopProductWithTagsDto {

private int productId;

private String description;

private List<Integer> tags;

public ShopProductWithTagsDto(int productId, String description, List<Integer> tags) {
    this.productId = productId;
    this.description = description;
    this.tags = tags;
}
}

0 个答案:

没有答案