【问题标题】:No property findAllEntries found for type Entry没有找到类型 Entry 的属性 findAllEntries
【发布时间】:2017-03-30 16:26:54
【问题描述】:

我不知道为什么 Spring 不喜欢我的代码:

我有Entry.java:

@Entity
@Table(name = "entries")
public class Entry {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "text")
    private String text;
}

EntryDao.java:

public interface EntryDao extends JpaRepository<Entry, Long> {
    List<Entry> findAllEntries();
}

EntryService.java:

@Service
public interface EntryService {

    List<Entry> findAllEntries();

}

EntryServiceImpl.java:

public class EntryServiceImpl implements EntryService {
    private EntryDao entryDao;
    private SessionFactory sessionFactory;

    @Override
    @SuppressWarnings("unchecked")
    public List<Entry> findAllEntries()  {
        Session session = this.sessionFactory.getCurrentSession();
        List<Entry> entries = session.createQuery("from entries").list();
        return entries;
    }
}

这段代码给了我一个错误:

org.springframework.beans.factory.BeanCreationException:创建名为“entryDao”的bean时出错:调用init方法失败;嵌套异常是 org.springframework.data.mapping.PropertyReferenceException: No property findAllEntries found for type Entry!

我不明白如何处理这个错误以及为什么会发生这个错误。

【问题讨论】:

  • @Service 放在实施中
  • @Ramanlfc 不,没有帮助
  • EntryDao 实现在哪里
  • 为什么要在服务类中访问数据库?应该在 dao 层完成。
  • JPA 无法解析您的方法List&lt;Entry&gt; findAllEntries();。顺便说一句,JPA 已经有一个方法 findAll() 可以为您提供表中的所有行。

标签: java spring


【解决方案1】:

您得到异常的根本原因是您违反了在 Spring Data JPA 中声明/创建查询的约定/规则。

The official docs of Spring Data JPA 提到:

Spring 数据存储库抽象的目标是显着减少为各种持久性存储实现数据访问层所需的样板代码量。

抽象的中心接口是Repository,要管理您的实体,您需要声明自己的接口Repository,JPA 将帮助您为这些接口创建代理实例。已经有一些基本的RepositoriesCrudRepositoryPagingAndSortingRepository 提供基本功能,从它们的名称可以看出,因此通过扩展这些基本功能,您将拥有许多基本方法。要定义更具体的访问方法,您需要遵循 JPA 提供的方式来创建查询:

  • 按照方法名称约定在界面中定义方法
  • 使用@Query 注解手动定义

对于第一种方法,the docs of Query Create有详细的说明,这里有一些关键思想:

该机制从方法中去除前缀​​ find...By、read...By、query...By、count...By 和 get...By 并开始解析其余部分。引入子句可以包含进一步的表达式,例如 Distinct 以在要创建的查询上设置不同的标志。但是,第一个 By 充当分隔符以指示实际条件的开始。在非常基本的级别上,您可以定义实体属性的条件并将它们与 And 和 Or 连接

简单来说,JPA 会解析方法名称并尝试找到相关属性来为您创建查询条件。

现在让我们看看你的代码,如果你只是想检索你所有的实体,你不需要定义你自己的方法,已经预定义了findAll方法,如果你想检索实体基于text 内容,它应该看起来像:

Entity findByText(String text)

但是您的方法findAllEntites 只是不匹配任何规则,所以 JPA 会向您抛出这样的错误消息。

【讨论】:

    【解决方案2】:

    正如@AbdullahWasi 所说,只需将SpringData 中现有的findAll() 方法用于您的代码即可。您可能希望在代码中放置 @Transactional 注释,但这取决于您的事务边界。

    只需从您的 Dao 中删除您的自定义方法。

    public interface EntryDao extends JpaRepository<Entry, Long> {
    }
    

    并使用默认的spring数据findAll

    @Transactional
    public class EntryServiceImpl implements EntryService {
        private EntryDao entryDao;
        private SessionFactory sessionFactory;
    
        @Override
        @SuppressWarnings("unchecked")
        public List<Entry> findAllEntries()  {
            return entryDao.findAll();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-13
      • 2019-01-05
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      • 2020-10-16
      • 1970-01-01
      • 2020-11-30
      相关资源
      最近更新 更多