【问题标题】:No property <method name> found for type <entityName>未找到类型 <entityName> 的属性 <method name>
【发布时间】:2018-10-05 07:58:37
【问题描述】:

我有以下实体:

@Entity
@Table(name = "MY_TABLE")
@Data
public class MyTable implements java.io.Serializable {

private static final long serialVersionUID = 3879471087851341216L;

@Id
@Column(name = "MY_ID")
private BigInteger myId;

@Column(name = "ANOTHER_COL")
private String anotherColumn;

然后我创建我的存储库:

@Repository
public interface MyTableRepository extends JpaRepository<MyTable, BigInteger> {

@Query(name = "select count(*) from MyTable where ...")
public Long getCountOf(@Param("myId") BigInteger myId);

但我一开始就收到了:

未找到类型 MyTable 的属性 getCountOf

我做错了什么?

【问题讨论】:

  • 您不需要提供明确的查询。 Spring-data-jpa 可以为您生成它。只需尝试将您的方法替换为long countByMyId(BigInteger myId);

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


【解决方案1】:

我怀疑方法的命名会误导spring-jpa。

spring-data 也可以为你自动生成计数查询,你不需要提供它,只要你遵循正确的命名约定:

@Repository
public interface MyTableRepository extends JpaRepository<MyTable, BigInteger> {

    long countByMyId(BigInteger myId);
}

【讨论】:

  • 似乎每个名字都会发生这种情况...我尝试调用方法“example”,但问题仍然存在
猜你喜欢
  • 2021-08-16
  • 1970-01-01
  • 2016-08-29
  • 2013-07-07
  • 2016-07-15
  • 1970-01-01
  • 1970-01-01
  • 2017-12-16
  • 1970-01-01
相关资源
最近更新 更多