英文:
Unable to locate DTO in Spring Boot Application
问题
以下是翻译好的内容:
我已经定义了我的DTO,但仍然无法定位它。
我已经在实体包中创建了DTO,并在需要它的地方导入了该包,在编译时没有任何错误,在运行代码时会在初始化JPA EntityManagerFactory之后出现错误,请帮我解决。
CartDTO 文件
package com.project.books.entity;
public class CartDTO {
private String name;
private String username;
private String status;
private int price;
public CartDTO() {
}
public CartDTO(String name, String username, String status, int price) {
this.name = name;
this.username = username;
this.status = status;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
cartRepository 文件
package com.project.books.dao;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import com.project.books.entity.Cart;
import com.project.books.entity.CartDTO;
public interface CartRepository extends JpaRepository<Cart, Integer> {
@Query("select c from Cart c where c.username = :username AND c.status = :status")
public List<Cart> findAllByUsernameAndStatus(@Param("username") String username,
@Param("status") String status);
@Query("select new CartDTO(c.name, c.username, c.status, sum(c.price)) from Cart c where c.username = :username AND c.status = :status GROUP BY c.username, c.status")
public CartDTO sumPriceByUsernameAndStatus(@Param("username") String username,
@Param("status") String status);
}
错误信息
无法找到类 [CartDTO]
at org.hibernate.hql.internal.ast.tree.ConstructorNode.resolveConstructor(ConstructorNode.java:177) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
...
在对象创建过程中,bean 名称为 'cartRepository' 的 FactoryBean 抛出异常;嵌套异常是 java.lang.IllegalArgumentException:验证方法公共抽象 com.project.books.dao.CartRepository.sumPriceByUsernameAndStatus(java.lang.String,java.lang.String) 失败!
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:176) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
...
验证方法公共抽象 com.project.books.dao.CartRepository.sumPriceByUsernameAndStatus(java.lang.String,java.lang.String) 失败!
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:93) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
无法找到类 [CartDTO] [select new CartDTO(c.name,c.username,c.status,sum(c.price)) from com.project.books.entity.Cart c where c.username = :username AND c.status = :status GROUP BY username,status]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
...
请帮我解决这个问题。我通过这个查询想要做的就是,我只需要在用户名为当前登录用户且状态为某个字符串的情况下,所有价格的总和。
英文:
I have defined my DTO then also it can't locate it.
I have created DTO in entities package and just imported the package wherever I needed it didn't give any error while compiling it gave error while running the code it occured after Initializing JPA EntityManagerFactory why so please help me out.
> CartDTO file
package com.project.books.entity;
public class CartDTO {
private String name;
private String username;
private String status;
private int price;
public CartDTO() {
}
public CartDTO(String name, String username, String status, int price) {
this.name = name;
this.username = username;
this.status = status;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
> cartRepository file
package com.project.books.dao;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import com.project.books.entity.Cart;
import com.project.books.entity.CartDTO;
public interface CartRepository extends JpaRepository<Cart,Integer> {
@Query("select c from Cart c where c.username = :username AND c.status = :status")
public List<Cart> findAllByUsernameAndStatus(@Param("username")String username,
@Param("status")String status);
@Query("select new CartDTO(c.name,c.username,c.status,sum(c.price)) from Cart c where c.username = :username AND c.status = :status GROUP BY username,status")
public CartDTO sumPriceByUsernameAndStatus(@Param("username")String username,
@Param("status")String status);
}
> Myerror
Unable to locate class [CartDTO]
at org.hibernate.hql.internal.ast.tree.ConstructorNode.resolveConstructor(ConstructorNode.java:177) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
at org.hibernate.hql.internal.ast.tree.ConstructorNode.prepare(ConstructorNode.java:144) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
at org.hibernate.hql.internal.ast.HqlSqlWalker.processConstructor(HqlSqlWalker.java:1258) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
Error creating bean with name 'cartRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract com.project.books.entity.CartDTO com.project.books.dao.CartRepository.sumPriceByUsernameAndStatus(java.lang.String,java.lang.String)!
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:176) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:101) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
Validation failed for query for method public abstract com.project.books.entity.CartDTO com.project.books.dao.CartRepository.sumPriceByUsernameAndStatus(java.lang.String,java.lang.String)!
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:93) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
Unable to locate class [CartDTO] [select new CartDTO(c.name,c.username,c.status,sum(c.price)) from com.project.books.entity.Cart c where c.username = :username AND c.status = :status GROUP BY username,status]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
at org.hibernate.hql.internal.ast.ErrorTracker.throwQueryException(ErrorTracker.java:93) ~[hibernate-core-5.4.18.Final.j
Please help me resolve it.
Let me tell you by this query what I meant to do I just needed sum of all prices where username is the user logged in and status is some string.
答案1
得分: 0
你必须使用完全限定名:
@Query("select new com.project.books.entity.CartDTO(c.name,c.username,c.status,sum(c.price)) from Cart c where c.username = :username AND c.status = :status GROUP BY username,status")
public CartDTO sumPriceByUsernameAndStatus(@Param("username")String username,
@Param("status")String status);
当您查询实体时,可以使用实体名称,如果没有设置类的简单名称。
但是对于DTO,没有这样的名称,所以您必须使用完全限定名。
此外,您必须更改构造函数为
public CartDTO(String name, String username, String status, long price) {
因为sum返回long类型。
英文:
You must use the fully qualified name:
@Query("select new com.project.books.entity.CartDTO(c.name,c.username,c.status,sum(c.price)) from Cart c where c.username = :username AND c.status = :status GROUP BY username,status")
public CartDTO sumPriceByUsernameAndStatus(@Param("username")String username,
@Param("status")String status);
When you query for Entities you can use the Entity name that's if not set the simple name of the Class.
But with DTOs there is no such name so you have to use the fully qualified name.
Additionally you have to change the constructor to
public CartDTO(String name, String username, String status, long price) {
Because sum returns long.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论