我有两个类 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;
}
}
]
答案 0 :(得分:0)
将返回类型更改为 List<Object[]>
。
@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();