我有两个表 Setup
和 Product
,并且我创建了一个 SetupProductJoin
来表示连接两个表时的模型。 productName
是来自 Product
表的字符串 (varchar),但我收到错误
错误
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.QueryException: could not instantiate class [com.nano.comp.domain.io.SetupProductJoin] from tuple; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryException: could not instantiate class [com.nano.comp.domain.io.SetupProductJoin] from tuple
public interface SetupProductJoinInterface {
String getProductName();
}
public abstract class SetupProductJoin implements SetupProductJoinInterface {
private String productName = "";
public SetupProductJoin() {
}
public SetupProductJoin(String productName) {
super();
this.productName = productName;
}
public String getproductName() {
return productName;
}
public void setproductName(String productName) {
this.productName = productName;
}
}
存储库
@Query(value = " SELECT new com.nano.comp.domain.io.SetupProductJoin(p.name as productName) " +
" FROM Setup bs " +
" INNER JOIN Product p ON p.id = bs.productId " +
" WHERE bs.wgId = ?1 "
)
List<SetupProductJoin> findByWgId(Long wgId);
Controller
List<SetupProductJoin> setup = setupRepository.findByWgId(wgId);
最佳答案
正如@M所指出的。 Deinum,SetupProductJoin
不应该是抽象的。
您的查询似乎有误。尝试以下查询:
@Query(value = " SELECT new com.nano.comp.domain.io.SetupProductJoin(p.name) " +
" FROM Setup bs " +
" INNER JOIN Product p ON p.id = bs.productId " +
" WHERE bs.wgId = :wgId "
)
List<SetupProductJoin> findByWgId(@Param("wgId") Long wgId);
还要检查您的 getter/setter 方法名称,它们应该采用驼峰命名法。
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
关于java - 无法从元组实例化类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67037244/