SOLVED: Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class

SOLVED: Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class

SOLVED: Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class:

Root cause:
If you are trying to populate result sets directly to your custom bean (not entity bean) then you must have public default constructor in your bean. See the below sample snippet to understand better.

 

Example:


@Query("SELECT new com.ngdeveloper.dto.SlugCountBean(t.tagSlug, count(c)) FROM Coupon c join c.tags t where c.couponEndDate >=CURRENT_DATE and t.tagType=:tagType GROUP BY t.tagSlug")
List<SlugCountBean> findCouponCountsTagWise(@Param("tagType") String tagType);

 

SlugCountBean.java Bean | which is causing the issue:


public class SlugCountBean {
private String slug;
private long count;

public SlugCountBean(String slug, long count) {
this.slug = slug;
this.count = count;
}

public String getSlug() {
return slug;
}

public void setSlug(String slug) {
this.slug = slug;
}

public long getCount() {
return count;
}

public void setCount(long count) {
this.count = count;
}

}

 

After creating the bean with public default constructor error got resolved.

SlugCountBean without any errors:


public class SlugCountBean {

private String slug;
private long count;

public SlugCountBean(){

}

public SlugCountBean(String slug, long count) {
this.slug = slug;
this.count = count;
}

public String getSlug() {
return slug;
}

public void setSlug(String slug) {
this.slug = slug;
}

public long getCount() {
return count;
}

public void setCount(long count) {
this.count = count;
}

}

114 total views, 4 views today

Leave a Reply

Your email address will not be published. Required fields are marked *