我如何解决在使用Spring JPA创建自定义查询方法时遇到的问题。

huangapple go评论65阅读模式
英文:

How can i resolve this issue that i am having when creating a custom query method in my repository using spring jpa

问题

抱歉,似乎无法运行您的应用程序。错误信息表明在您的 TransactionRepo 仓库中的 findByFromAccount 方法存在问题。错误提示中指出,在 Transaction 实体类中不存在名为 fromAccount 的属性。根据您提供的 Transaction 实体类的代码,Transaction 类中并没有名为 fromAccount 的属性,而是存在名为 fromAcct 的属性。这可能导致查询方法无法正确识别。

您可以尝试更改 TransactionRepo 中的查询方法为 findByFromAcct,以便与 Transaction 实体类中的属性名匹配。方法应该如下所示:

List<Transaction> findByFromAcct(Account fromAccount);

请确保在更改后,仍然与数据库实际存储的字段名称和实体类属性名称相匹配。这应该解决由于方法和实体属性名称不匹配而导致的错误。

英文:

Hello im trying to create a custom query method in my repository but when i run my application i get this error

 Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;transactionRepo&#39; 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 &#39;fromAccount&#39; found for type &#39;Transaction&#39;; 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 &#39;fromAccount&#39; found for type &#39;Transaction&#39;

Transaction Model

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

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


}

Transaction Repo

     @Repository
     public interface TransactionRepo extends CrudRepository&lt;Transaction, Integer&gt; {

         List&lt;Transaction&gt; findByFromAccount(Account fromAccount);

}

ive tried changing the name of the method and also the parameter type that is passed to it but i still get the same error

答案1

得分: 1

在存储库中使用的属性名称需要与实体中的属性名称匹配。
错误消息说:未找到类型为'Transaction'的属性'fromAccount'
如果您检查您的实体,这是正确的。只有一个属性fromAcct

因此,存储库方法应该命名为findByFromAcct

英文:

The property names used in the repository need to match those in your entity.
The error message says: No property &#39;fromAccount&#39; found for type &#39;Transaction&#39;
And if you check your entity, this is true. There is only a property fromAcct.

Therefore the repository method should be named findByFromAcct

huangapple
  • 本文由 发表于 2023年2月16日 11:50:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定