为啥无法在类上找到适当的构造函数
Posted
技术标签:
【中文标题】为啥无法在类上找到适当的构造函数【英文标题】:Hibernate: Unable to locate appropriate constructor on class in hql为什么无法在类上找到适当的构造函数 【发布时间】:2021-10-25 08:26:30 【问题描述】:我收到错误:
org.hibernate.hql.internal.ast.QuerySyntaxException:无法在类 [com.meetzen.Dto.profileJoinDto] 上找到适当的构造函数。预期的参数是: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]
代码如下:
profileEntity
package com.meetzen.Entity;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "profile_master")
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
注册实体
package com.meetzen.Entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "registration_master")
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
package com.meetzen.Dto;
import javax.persistence.Id;
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
package com.meetzen.Repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import com.meetzen.Dto.profileJoinDto;
import com.meetzen.Entity.profileEntity;
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);
堆栈跟踪
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
【问题讨论】:
【参考方案1】:…
FROM profileEntity p JOIN p.registrationEntity p
…
您在这里使用了两次相同的别名 (p
)。试试FROM profileEntity p JOIN p.registrationEntity r
。
【讨论】:
【参考方案2】:我得到了答案,我将我的 jpql 查询更改为 sql 查询并将我的 profileJoinDto 类转换为接口...
代码更改:
profileJoinDto:
package com.meetzen.Dto;
public interface profileJoinDto
public Integer getU_id();
public Integer getP_id();
public String getEmail();
public String getUsername();
public String getPassword();
public String getContact();
public String getDate();
public String getWebsite();
public String getBio();
public String getGender();
下面的jpql查询改为sql查询
profileRepo:
package com.meetzen.Repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import com.meetzen.Dto.profileJoinDto;
import com.meetzen.Entity.profileEntity;
public interface profileRepo extends JpaRepository<profileEntity, Integer>
//JPQL QUERY
//@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")
// SQL QUERY
@Query(nativeQuery = true, value = "SELECT * FROM registration_master rm INNER JOIN profile_master pm ON rm.u_id = pm.user_id WHERE rm.u_id = ?")
profileJoinDto findById(int id);
【讨论】:
以上是关于为啥无法在类上找到适当的构造函数的主要内容,如果未能解决你的问题,请参考以下文章
JPA,Hibernate:在现有工作 DTO 中添加新变量时“无法在类上找到适当的构造函数”
为啥在父类的构造函数中调用重载函数时,在 ES6 类上设置属性不起作用
错误 C2512:没有适当的默认构造函数可用 - 为啥在构造函数中初始化属性?
为啥在类构造函数中使用 setState 方法时 React 会抛出错误?