如何使用JOIN转换HQL本机查询的结果

时间:2019-03-13 17:46:24

标签: hibernate spring-boot jpa spring-data

我正在使用Spring Boot / Data,使用本机查询进行休眠,以避免实体中昂贵的JOIN(我在使用@Any注释)。

我需要连接一些表,并从映射到我的域模型的特定表中返回结果。

放置在JpaRepository中的查询应阐明这个想法:

@Query(value = "SELECT row.* FROM Document AS d JOIN DocumentRow AS row ON row.document_id=d.id WHERE d.contact_id=:contactId AND row.productGroup='F'", nativeQuery = true)
public Page<DocumentRow> getLastPurchasedFrames(@Param("contactId") long contactId, Pageable pageable);

我应该返回DocumentRow对象的类型化结果。我写的查询抛出此异常:

13/03/2019 22:07:02,147 ERROR http-nio-8082-exec-2 [dispatcherServlet]:182 - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object[]] to type [cloud.optix.server.model.accounting.documents.rows.DocumentRow] for value '{1, e2dd17bb-3555-4e43-b66b-11a4e666dd5a, 2019-03-13 19:34:23.752, e2dd17bb-3555-4e43-b66b-11a4e666dd5a, 2019-03-13 19:37:54.568, 79bb9094-e199-4c88-bdae-a5f4e3f38d77, 2, 81.967, AVM Try Change 503953 53, 1, false, 0.00, F, 4780, 5CF03953, 8029224451988, null, 31.500, 31.500, 1, 0, null, 1, 18.03, 100.00, 81.967, 1, null, null, 1}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.math.BigInteger] to type [model.accounting.documents.rows.DocumentRow]] with root cause
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.math.BigInteger] to type [server.model.accounting.documents.rows.DocumentRow]

不使用本机查询,选择行将自动获得一个DocumentRow对象。如何通过本机查询完成同一件事?

================================================ ======================

这是DocumentRow bean:

@Entity
@EntityListeners({DocumentRowListener.class})
public class DocumentRow extends AbstractEntity {

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private Document document;

    @Column(nullable = false, columnDefinition = "INT DEFAULT 1")
    private int index = 1;

    private String rowGroup;

    @JsonDeserialize(using = ProductUriDeserializer.class)
    @Any(fetch = FetchType.EAGER, metaDef = "ProductDocumentRowDef", metaColumn = @Column(name = "productGroup"), optional = true)
    @AnyMetaDef(name = "ProductDocumentRowDef", metaType = "string", idType = "long", metaValues = {@MetaValue(value = "OL", targetEntity = OphthalmicLens.class),
            @MetaValue(value = "F", targetEntity = Frame.class), @MetaValue(value = "CL", targetEntity = ContactLens.class), @MetaValue(value = "GP", targetEntity = GenericProduct.class)})
    @JoinColumn(name = "product_id")
    private Product product;

    @Size(max = 255)
    @Column(length = 255)
    @ColumnTransformer(write = "UPPER(?)")
    private String productVariant;

    @JsonDeserialize(using = DocumentUriDeserializer.class)
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private Document linkedDocument;

    @JsonDeserialize(using = DocumentRowUriDeserializer.class)
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private DocumentRow linkedRow;

    @Size(min = 4, max = 25)
    @Column(length = 30)
    private String productSku;

    @Size(min = 9, max = 13)
    @Column(length = 30)
    @Pattern(regexp = "[0-9]+")
    private String productUpc;

    @Size(max = 255)
    @NotBlank
    @Column(nullable = false)
    private String description;

    @NotNull
    @Min(0)
    @Column(nullable = false)
    private int qty = 1;

    @Min(0)
    @Column(nullable = false)
    private int shippedQty = 0;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @Generated(value = GenerationTime.ALWAYS)
    @Column(columnDefinition = "INT(11) AS (qty-shippedQty) VIRTUAL NOT NULL")
    private int qtyToShip;

    @NotNull
    @Min(value = 0)
    @ColumnDefault("0.000")
    @Column(nullable = false, scale = 3, columnDefinition = "DECIMAL(13,3)")
    private BigDecimal unitPrice = BigDecimal.ZERO;

    @Column(scale = 3, columnDefinition = "DECIMAL(13,3)")
    private BigDecimal purchaseUnitPrice;

    @NotNull
    @Min(value = 0)
    @Max(value = 99)
    @ColumnDefault("0.00")
    @Column(nullable = false, scale = 2, columnDefinition = "DECIMAL(4,2)")
    private BigDecimal percentageDiscount = BigDecimal.ZERO;

    @NotNull
    @Min(value = 0)
    @ColumnDefault("0.000")
    @Column(nullable = false, scale = 3, columnDefinition = "DECIMAL(13,3)")
    private BigDecimal amount = BigDecimal.ZERO;

    @Min(value = 0)
    @Generated(value = GenerationTime.ALWAYS)
    @Column(columnDefinition = "DECIMAL(13,3) AS (purchaseUnitPrice*qty)")
    private BigDecimal purchaseAmount;

    @JsonDeserialize(using = TaxRateUriDeserializer.class)
    @ManyToOne(fetch = FetchType.EAGER, optional = true)
    private TaxRate taxRate;

    @NotNull
    @Min(value = 0)
    @ColumnDefault("0.00")
    @Column(nullable = false, scale = 2, columnDefinition = "DECIMAL(12,2)")
    private BigDecimal taxAmount = BigDecimal.ZERO;

    @Generated(value = GenerationTime.ALWAYS)
    @Column(columnDefinition = "DECIMAL(12,2) AS (amount+taxAmount)", scale = 2)
    private BigDecimal totalAmount;

    @NotNull
    @Column(nullable = false, columnDefinition = "BIT DEFAULT 0")
    private boolean note = false;

    @Transient
    private Map<String, Object> specificationDetails;

    @Transient
    private String specificationDescription;

0 个答案:

没有答案