英文:
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 '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'
Transaction Model
@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;
}
Transaction Repo
@Repository
public interface TransactionRepo extends CrudRepository<Transaction, Integer> {
List<Transaction> 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 'fromAccount' found for type 'Transaction'
And if you check your entity, this is true. There is only a property fromAcct
.
Therefore the repository method should be named findByFromAcct
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论