【问题标题】:Could not create query for public Spring JPA无法为公共 Spring JPA 创建查询
【发布时间】:2021-07-20 14:21:22
【问题描述】:

我有两个类 Employee 和 Asset。他们有关系@ManyToOne。 当我尝试离开连接表时,控制台显示运行时错误。 请

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'assetController' defined in file [C:\Users\User\Desktop\material-assets\target\classes\com\alsecotask\materialassets\controller\AssetController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'assetService' defined in file [C:\Users\User\Desktop\material-assets\target\classes\com\alsecotask\materialassets\service\AssetService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'assetRepository' defined in com.alsecotask.materialassets.repository.AssetRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List com.alsecotask.materialassets.repository.AssetRepository.getAssetsByEmployee()! Reason: Validation failed for query for method public abstract java.util.List com.alsecotask.materialassets.repository.AssetRepository.getAssetsByEmployee()!; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.alsecotask.materialassets.repository.AssetRepository.getAssetsByEmployee()!

我在其中编写查询的 AssetReposiory

public interface AssetRepository extends JpaRepository<Asset, Long> {

Long countAssetByEmployee_Id(Long id);

@Query("SELECT Employee.id, Employee.firstName, sum(Asset .price), count(Asset .price)\n" +
        "FROM Asset \n" +
        "         LEFT JOIN Employee \n" +
        "                   ON Employee .id = Asset .employee.id group by Employee .id\n")
List<?> getAssetsByEmployee();

@Query(value = "SELECT * FROM Asset WHERE name = ?1", nativeQuery = true)
Asset findByNameQuery(String name);

}

资产类别

public class Asset {
@Id
@GeneratedValue
private Long id;
private String name;
private double price;
@ManyToOne
@JoinColumn(name = "employee_id", nullable = false)
private Employee employee;}

员工类

public class Employee {
@Id
@SequenceGenerator(
        name = "employee_sequence",
        sequenceName = "employee_sequence",
        allocationSize = 1
)
@GeneratedValue(
        strategy = GenerationType.SEQUENCE,
        generator = "employee_sequence"
)
private Long id;
private String firstName;
private String lastName;
public Employee(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
}

}

]

【问题讨论】:

  • 请勿将代码发布为屏幕截图,而应将其发布为格式化为代码的文本。始终包含完整的堆栈跟踪。

标签: spring spring-boot spring-data-jpa


【解决方案1】:

将返回类型更改为List&lt;Object[]&gt;

@Query(value="SELECT Employee.id, Employee.firstName, sum(Asset 
.price), count(Asset .price)\n" +
    "FROM Asset \n" +
    "         LEFT JOIN Employee \n" +
    "                   ON Employee .id = Asset .employee.id group by 
Employee.id\n", nativeQuery= true)
List<Object[]> getAssetsByEmployee();

【讨论】:

    猜你喜欢
    • 2021-12-01
    • 1970-01-01
    • 2017-03-07
    • 2021-08-03
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 2019-08-26
    • 2020-02-08
    相关资源
    最近更新 更多