Faheem azaz Bhanej
Faheem azaz Bhanej

Reputation: 2396

Hibernate: Unable to locate appropriate constructor on class in hql

I get error while run project:

org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [com.meetzen.Dto.profileJoinDto].

Here down is my code:

Entity

public class profileEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int p_id;
    private String website;
    private String bio;
    private String gender;

    @OneToOne(cascade = CascadeType.ALL, targetEntity = registrationEntity.class)
    @JoinColumn(name = "user_id", referencedColumnName = "u_id")
    private registrationEntity registrationEntity;

    // getter setter

}

public class registrationEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int u_id;
    @Column(unique = true)
    private String email;
    private String username;
    private String password;
    private String contact;
    private String date;

    // getter setter
}

profileJoinDto

public class profileJoinDto {
    private Integer u_id;
    private Integer p_id;
    private String email;
    private String username;
    private String password;
    private String contact;
    private String date;
    private String website;
    private String bio;
    private String gender;

    // getter setter
}

profileRepo

public interface profileRepo extends JpaRepository<profileEntity, Integer>{
    @Query("SELECT new com.meetzen.Dto.profileJoinDto(r.username, r.email, r.contact, p.bio, p.website, p.gender) FROM profileEntity p JOIN p.registrationEntity r WHERE r.u_id = ?1")
    profileJoinDto findById(int id);
}

Stacktrace

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [com.meetzen.Dto.profileJoinDto]. Expected arguments are: java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String [SELECT new com.meetzen.Dto.profileJoinDto(r.username, r.email, r.contact, p.bio, p.website, p.gender) FROM com.meetzen.Entity.profileEntity p JOIN p.registrationEntity r WHERE r.u_id = ?1]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.hql.internal.ast.ErrorTracker.throwQueryException(ErrorTracker.java:93) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:282) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:192) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:144) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:113) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:73) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:162) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:613) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:725) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    ... 86 common frames omitted

Upvotes: 1

Views: 2045

Answers (1)

slauth
slauth

Reputation: 3178

FROM profileEntity p JOIN p.registrationEntity p

You are using the same alias (p) twice here. Try with FROM profileEntity p JOIN p.registrationEntity r.

Upvotes: 1

Related Questions