我正在尝试自己学习Spring,我打算通过创建一个博客网络应用程序来做到这一点.我已经有了基本的博客功能,这是一个显示博客帖子的页面,以及一个提交表单的页面.显示博客帖子的页面显示了最新的博客文章,以及数据库中所有博客帖子的标题列表.
为了从数据库中获取博客文章的有序列表,我首先在我的Repository界面中创建了一个sql查询.这工作,但现在我想使用的功能,我可以在界面中键入方法名称,而不是硬编码的SQL.我在这里找到了支持的关键字:http://docs.spring.io/spring-data/jpa/docs/1.4.2.RELEASE/reference/html/jpa.repositories.html#jpa.query-methods.query-creation,并试图实现它.
所以使用我的方法,findAllOrderByIdDesc()我试图实现与我的SQL查询相同的事情.我不确定为什么它不起作用,我想我正确使用了关键字?
stackstrace(我不完全理解):
引起:org.springframework.data.mapping.PropertyReferenceException:在org.springframework.data.mapping.PropertyPath找不到int类型的属性desc.(PropertyPath.java:75)在org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)在org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)在org.springframework.data.mapping.PropertyPath.create (PropertyPath.java:330)在org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:353)在org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)在org.springframework. org.springframework.data.repository.query.parser.Part上的org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:245)中的data.mapping.PropertyPath.from(PropertyPath.java:271).(部分) .java:72)org.springframework.data.repository.query.parser.PartTree $ OrPart.(PartTree.java:188)org.springframework.data.repository.query.parser.PartTree $ Predicate.buildTree(PartTree.java:277)org.springframework.data.repository.query.parser.PartTree $ Predicate.(PartTree.java:257)org.springframework.data.repository.query.parser.PartTree.( PartTree.java:71)org.springframework.data.jpa.repository.query.PartTreeJpaQuery.(PartTreeJpaQuery.java:57)at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy $ CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java) :90)org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy $ CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:162)org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy $ AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java) :68)org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.(RepositoryFactorySupport.java:290)org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySup)port.java:158)org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:162)org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java: 44)org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:144)... 32更多
我的存储库界面:
public interface PostRepository extends CrudRepository<Post, Integer> {
@Query("select p from Post p order by p.id desc")
Iterable<Post> findLastFirst();
Iterable<Post> findAllOrderByIdDesc();
}
Run Code Online (Sandbox Code Playgroud)
我的帖子实体:
@Entity
public class Post {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private Date date;
private String title;
@Lob
private String body;
protected Post() {
date = new Date();
}
public Post(String title, String body) {
this.date = new Date();
this.title = title;
this.body = body;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getDate() {
return date;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
Run Code Online (Sandbox Code Playgroud)
Pau*_*oso 97
我知道这有点晚了,但这可能有助于其他人.
只要把BY前ORDER,像这样:findAllByOrderByIdDesc
它应该工作.
小智 12
有一个扩展名为CrudRepository PagingAndSortingRepository
public interface PostRepository extends PagingAndSortingRepository<Post, Integer> {}
Run Code Online (Sandbox Code Playgroud)
然后只需调用.findAll(new Sort(Sort.Direction.DESC,"id")); 而不是findAllOrderByIdDesc();
我知道我玩游戏有点迟了,但是..
另一个解决方法是更正存储库中命名查询的语法。
你有: Iterable<Post> findAllOrderByIdDesc();
什么时候应该是: Page<Post> findAllByOrderByIdDesc();
请注意,在By之后添加了关键字findAll。这标志着过滤语句的开始。
| 归档时间: |
|
| 查看次数: |
16171 次 |
| 最近记录: |