简体   繁体   中英

Hibernate oneToOne join with additional criteria

I need to do a join in hibernate, but I want to exclude some results from being found. I've tried using @JoinColumnsOrFormulas , but I still get all results

@OneToOne
@JoinColumnsOrFormulas({
        @JoinColumnOrFormula(formula = @JoinFormula(value= "(select a.seller_sku from de_products a where a.asin = 'a' and a.product_name != '' and a.seller_sku != '' and a.seller_sku = sku)", referencedColumnName = "seller_sku")),
        @JoinColumnOrFormula(column = @JoinColumn(name = "sku", referencedColumnName = "seller_sku", insertable = false, updatable = false))
})
public DeProducts getDeProduct() {
    return deProduct;
}

If I try it without the column definition

@OneToOne
@JoinColumnsOrFormulas({
        @JoinColumnOrFormula(formula = @JoinFormula(value= "(select a.seller_sku from de_products a where a.asin = 'a' and a.product_name != '' and a.seller_sku != '' and a.seller_sku = sku)", referencedColumnName = "seller_sku"))
})
public DeProducts getDeProduct() {
    return deProduct;
}

I get an NullPointerException on application startup, at

org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory

How do I do a join with spring-boot (1.4) hibernate (5.2.2.Final) with additional criterias/exclusions?

I think I found a solution that works. Hibernate seems to ignore the @JoinForumula when referencing the same column in the formula and in the @JoinColumn annotation. When referencing another column in the formula - eg id , it works.

@OneToOne
@JoinColumnsOrFormulas({
        @JoinColumnOrFormula(formula = @JoinFormula(value = "(select a.id from de_products a where a.asin != '' and a.product_name = 'a' and a.seller_sku = sku)", 
              referencedColumnName = "id")),
        @JoinColumnOrFormula(column = @JoinColumn(name = "sku", referencedColumnName = "seller_sku", insertable = false, updatable = false))
})
public DeProducts getDeProduct() {
    return deProduct;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM