简体   繁体   中英

Path expected for join hql hibernate

I know this question exists in SO. However I have read many posts looking for this error and I can't solve my problem.

I try to execute an inner join for "many to many" relationships which the middle table has to underline like promotion_code_product, but I write on @Query something like this and failed, the error is something like Path expected for join hql hibernate

here is my query :

 @Query("SELECT p.serviceName, p.category, v.code from Product p " 
    + " INNER JOIN PromotionCodeCategory cp ON p.id = cp.productId " 
    + " INNER JOIN Voucher v on v.id = cp.promotionCodeId " 
    + " WHERE v.code = ?1")

i also tried something like this :

@Query("SELECT p.serviceName, p.category, v.code from Product p INNER JOIN PromotionCodeProduct cp ON p.id = cp.productId INNER JOIN Voucher v on v.id = cp.promotionCodeId WHERE v.code = ?1")

entity like this :

@Table(name = "VOUCHER")
@DynamicUpdate
//@Data
@Setter
@Getter
public class Voucher extends Base {

    @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    @JoinTable(name = "PROMOTION_CODE_PRODUCT",
               joinColumns =  @JoinColumn(name = "PROMOTION_CODE_ID"),
               inverseJoinColumns = @JoinColumn(name = "PRODUCT_ID"))
    private Set<Product> products;

    @Column(name = "CODE", unique = true)
    private String code;

    @Column(name = "TYPE", nullable = false)
    private String type;

    @Lob
    private String descriptions;

    public int hasCode() {
        return Objects.hashCode(getId());
    }

}



@Entity
@Table(name = "PRODUCT")
//@Data
@Setter
@Getter
@DynamicUpdate
public class Product extends Base {

    @ManyToMany(mappedBy = "products", fetch = FetchType.EAGER)
    private Set<Voucher> promotionCodes;

    @Column(name = "SERVICE_NAME", nullable = false)
    private String serviceName;

    @Column(name = "CATEGORY")
    private String category;

    @Column(name = "DESCRIPTION")
    private String description;

    public int hasCode() {
        return Objects.hashCode(getId());
    }

}

Bear in mind that hql joins are not the same as regular sql joins.

With hql you perform queries on entities instead of tables.

So joins are performed for fields contained within an entity.

Assuming your product class contains a List or Set of PromotionCodeCategory called "categories" and your PromotionCodeCategory contains a List or Set of Voucher called "vouchers", then your query should look like this:

@Query("SELECT p.serviceName, p.category, v.code from Product p " 
    + " INNER JOIN p.categories AS cp " 
    + " INNER JOIN cp.vouchers as v " 
    + " WHERE v.code = xxx")

Edit

It's been noted that there are only two entities: Product and Voucher. Hence your classes should look like this:

@Entity
@Table(name = "products")
public class Product { 
  
    @ManyToMany(cascade = { CascadeType.ALL })
    @JoinTable(
        name = "product_voucher",   // insert join table name here
        joinColumns = { @JoinColumn(name = "product_id") }, // insert product column name from join table here 
        inverseJoinColumns = { @JoinColumn(name = "voucher_id") } // // insert voucher column name from join table here
    )
    Set<Voucher> vouchers = new HashSet<>();
    
    // standard constructor/getters/setters
}

    
@Entity
@Table(name = "vouchers")
public class Voucher {    
  
    @ManyToMany(mappedBy = "vouchers")
    private Set<Product> products = new HashSet<>();
     
    // standard constructors/getters/setters   
}

Then the join query should look like this:

@Query("SELECT p.serviceName, p.category, v.code from Product p " 
    + " INNER JOIN p.vouchers AS v " 
    + " WHERE v.code = xxx")

Hope this helps

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