Home > database >  Unable to locate appropriate constructor Error
Unable to locate appropriate constructor Error

Time:08-15

I am trying to run my gradle springboot project for a query which I changed , but I am not able to find what error I am doing. Can anybody point out what I am doing wrong in this.

Here is the new class I created

    @Embeddable
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PartsByPartNumber {

    String partNumber;
    
    List<Part> parts;

}

And this is the query in my repo class which I have written

@Repository
public interface PartRepo
        extends JpaRepository<Part, Integer>, JpaSpecificationExecutor<Part>, QueryByExampleExecutor<Part> {

......
    @Query("    SELECT new com.ge.digital.oa.moa.domain.PartsByPartNumber(p.partNumber, p) "
              " FROM Part p "
              " GROUP BY p.partNumber ")
    public List<PartsByPartNumber> findByPartsByPartNumber();
}

But when I run gradle bootrun I am getting this error

2022-08-12 12:16:55 [restartedMain] ERROR o.h.h.i.a.ErrorTracker.reportError -  Unable to locate appropriate constructor on class [com.ge.digital.oa.moa.domain.PartsByPartNumber]. Expected arguments are: java.lang.String, com.ge.digital.oa.moa.domain.Part
[cause=org.hibernate.PropertyNotFoundException: no appropriate constructor in class: com.ge.digital.oa.moa.domain.PartsByPartNumber]
2022-08-12 12:16:55 [restartedMain] ERROR o.h.h.i.a.ErrorTracker.reportError -  Unable to locate appropriate constructor on class [com.ge.digital.oa.moa.domain.PartsByPartNumber]. Expected arguments are: java.lang.String, com.ge.digital.oa.moa.domain.Part
[cause=org.hibernate.PropertyNotFoundException: no appropriate constructor in class: com.ge.digital.oa.moa.domain.PartsByPartNumber]
org.hibernate.hql.internal.ast.DetailedSemanticException: Unable to locate appropriate constructor on class [com.ge.digital.oa.moa.domain.PartsByPartNumber]. Expected arguments are: java.lang.String, com.ge.digital.oa.moa.domain.Part
        at org.hibernate.hql.internal.ast.tree.ConstructorNode.resolveConstructor(ConstructorNode.java:182) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
        at org.hibernate.hql.internal.ast.tree.ConstructorNode.prepare(ConstructorNode.java:144) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
        at org.hibernate.hql.internal.ast.HqlSqlWalker.processConstructor(HqlSqlWalker.java:1245) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2366) [hibernate-core-5.3.9.Final.jar:5.3.9.Final]
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:2232) [hibernate-core-5.3.9.Final.jar:5.3.9.Final]
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1503) [hibernate-core-5.3.9.Final.jar:5.3.9.Final]
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:585) [hibernate-core-5.3.9.Final.jar:5.3.9.Final]
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:313) [hibernate-core-5.3.9.Final.jar:5.3.9.Final]
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:261) [hibernate-core-5.3.9.Final.jar:5.3.9.Final]
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:271) [hibernate-core-5.3.9.Final.jar:5.3.9.Final]
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:191) [hibernate-core-5.3.9.Final.jar:5.3.9.Final]
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:143) [hibernate-core-5.3.9.Final.jar:5.3.9.Final]
        at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:119) [hibernate-core-5.3.9.Final.jar:5.3.9.Final]
        at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80) [hibernate-core-5.3.9.Final.jar:5.3.9.Final]
        at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153) [hibernate-core-5.3.9.Final.jar:5.3.9.Final]
        at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:605) [hibernate-core-5.3.9.Final.jar:5.3.9.Final]
        at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:714) [hibernate-core-5.3.9.Final.jar:5.3.9.Final]
        at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23) [hibernate-core-5.3.9.Final.jar:5.3.9.Final]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_251]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_251]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_251]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_251]
        at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:350) [spring-orm-5.1.6.RELEASE.jar:5.1.6.RELEASE]
        at com.sun.proxy.$Proxy175.createQuery(Unknown Source) [na:na]
        at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:87) [spring-data-jpa-2.1.6.RELEASE.jar:2.1.6.RELEASE]
        at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:63) [spring-data-jpa-2.1.6.RELEASE.jar:2.1.6.RELEASE]
        at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:76) [spring-data-jpa-2.1.6.RELEASE.jar:2.1.6.RELEASE]
        at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromQueryAnnotation(JpaQueryFactory.java:56) [spring-data-jpa-2.1.6.RELEASE.jar:2.1.6.RELEASE]
        at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:142) [spring-data-jpa-2.1.6.RELEASE.jar:2.1.6.RELEASE]
        at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:209) [spring-data-jpa-2.1.6.RELEASE.jar:2.1.6.RELEASE]
        at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:79) [spring-data-jpa-2.1.6.RELEASE.jar:2.1.6.RELEASE]
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lookupQuery(RepositoryFactorySupport.java:566) [spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]

CodePudding user response:

Add the annotation @NoArgsConstructor to the class.

CodePudding user response:

Your class PartsByPartNumber has a field parts which is of type List<Part>, so the created constructor will have the types String and List<Part>. In the HQL query of your repository you are referring, through the new com.ge.digital.oa.moa.domain.PartsByPartNumber(p.partNumber, p) constructor expression, to a constructor that should be of the type String and Part though, which simply doesn't exist.

The thing you want to do is AFAIK not directly possible with Spring Data JPA. You need to use Hibernate APIs, e.g. ResultListTransformer or fetch a list of Part and do the construction and grouping of objects yourself in Java code.

  • Related