Java 未找到类型项的属性findAllEntries
我不知道为什么Spring不喜欢我的代码: 我有Java 未找到类型项的属性findAllEntries,java,spring,Java,Spring,我不知道为什么Spring不喜欢我的代码: 我有Entry.java: @Entity @Table(name = "entries") public class Entry { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "text") private String text; } public interface En
Entry.java
:
@Entity
@Table(name = "entries")
public class Entry {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "text")
private String text;
}
public interface EntryDao extends JpaRepository<Entry, Long> {
List<Entry> findAllEntries();
}
@Service
public interface EntryService {
List<Entry> findAllEntries();
}
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;
}
}
EntryDao.java
:
@Entity
@Table(name = "entries")
public class Entry {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "text")
private String text;
}
public interface EntryDao extends JpaRepository<Entry, Long> {
List<Entry> findAllEntries();
}
@Service
public interface EntryService {
List<Entry> findAllEntries();
}
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;
}
}
EntryServiceImpl.java
:
@Entity
@Table(name = "entries")
public class Entry {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "text")
private String text;
}
public interface EntryDao extends JpaRepository<Entry, Long> {
List<Entry> findAllEntries();
}
@Service
public interface EntryService {
List<Entry> findAllEntries();
}
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;
}
}
公共类EntryServiceImpl实现EntryService{
私人入口道入口道;
私人会话工厂会话工厂;
@凌驾
@抑制警告(“未选中”)
公共列表findAllEntries(){
会话会话=this.sessionFactory.getCurrentSession();
列表条目=session.createQuery(“来自条目”).List();
返回条目;
}
}
此代码给了我一个错误:
org.springframework.beans.factory.BeanCreationException:创建名为“entryDao”的bean时出错:调用init方法失败;嵌套异常为org.springframework.data.mapping.PropertyReferenceException:未找到类型项的属性findAllEntries
我不明白如何处理这个错误以及为什么会发生这个错误。正如@AbdullahWasi所说,只需使用SpringData中现有的
findAll()
方法来编写代码。您可能希望在代码中放置@Transactional
注释,但这取决于您的事务边界
只需从Dao中删除自定义方法
public interface EntryDao extends JpaRepository<Entry, Long> {
}
正如@AbdullahWasi所说的,只需使用SpringData中现有的
findAll()
方法即可。您可能希望在代码中放置@Transactional
注释,但这取决于您的事务边界
只需从Dao中删除自定义方法
public interface EntryDao extends JpaRepository<Entry, Long> {
}
出现异常的根本原因是您违反了在SpringDataJPA中声明/创建查询的约定/规则 提到: Spring数据存储库抽象的目标是显著减少为各种持久性存储实现数据访问层所需的样板代码量 抽象的中心接口是
Repository
,要管理您的实体,您需要声明自己的Repository
接口,JPA将帮助您为这些接口创建代理实例。已经有一些基本的存储库
,如crudepository
或paging和sortingrepository
来提供基本功能,从它们的名称可以看出,因此通过扩展这些基本功能,您将拥有许多基本方法。要定义更具体的访问方法,您需要遵循JPA提供的创建查询的方法:
- 按照方法名称约定在接口中定义方法
- 使用@Query注释手动定义它
findAll
方法,如果您想基于文本
内容检索实体,应该是这样的:
Entity findByText(String text)
但是您的方法
findAllEntites
与任何规则都不匹配,因此JPA会向您抛出这样一条错误消息。导致异常的根本原因是您违反了在Spring数据JPA中声明/创建查询的约定/规则
提到:
Spring数据存储库抽象的目标是显著减少为各种持久性存储实现数据访问层所需的样板代码量
抽象的中心接口是Repository
,要管理您的实体,您需要声明自己的Repository
接口,JPA将帮助您为这些接口创建代理实例。已经有一些基本的存储库
,如crudepository
或paging和sortingrepository
来提供基本功能,从它们的名称可以看出,因此通过扩展这些基本功能,您将拥有许多基本方法。要定义更具体的访问方法,您需要遵循JPA提供的创建查询的方法:
- 按照方法名称约定在接口中定义方法
- 使用@Query注释手动定义它
findAll
方法,如果您想基于文本
内容检索实体,应该是这样的:
Entity findByText(String text)
但是您的方法
findAllEntites
与任何规则都不匹配,因此JPA会向您抛出这样一条错误消息。打开@Service
implementation@Ramanlfc不,没用