java 当我使用springjpa在存储库中创建自定义查询方法时,我该如何解决这个问题呢

nc1teljy  于 2023-02-18  发布在  Java
关注(0)|答案(1)|浏览(118)

你好,我正在尝试在我的存储库中创建一个自定义查询方法,但是当我运行我的应用程序时,我得到了这个错误

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionRepo' defined in com.base.BaseDependencies.Repository.TransactionRepo 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.base.BaseDependencies.Repository.TransactionRepo.findByFromAccount(com.base.BaseDependencies.Models.Account); Reason: Failed to create query for method public abstract java.util.List com.base.BaseDependencies.Repository.TransactionRepo.findByFromAccount(com.base.BaseDependencies.Models.Account)! No property 'fromAccount' found for type 'Transaction'; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.base.BaseDependencies.Repository.TransactionRepo.findByFromAccount(com.base.BaseDependencies.Models.Account)! No property 'fromAccount' found for type 'Transaction'

交易模型

@Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Entity
    @Table(name="Transactions")
    public class Transaction {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="trans_id",updatable = false, nullable = false, unique = true)
    private int transId;

    private String transType;
    
    private double transAmount;
    
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "account_number")
    @JsonManagedReference
    private Account fromAcct;
    
    private long toAcct;

}

交易回购

@Repository
     public interface TransactionRepo extends CrudRepository<Transaction, Integer> {

         List<Transaction> findByFromAccount(Account fromAccount);

}

我试过改变方法的名称和传递给它的参数类型,但还是得到同样的错误

jaxagkaj

jaxagkaj1#

存储库中使用的属性名称需要与实体中的属性名称匹配。错误消息如下:如果你检查你的实体,这是真的,只有一个属性。
因此,存储库方法应命名为findByFromAcct

相关问题