【发布时间】:2013-12-20 21:13:57
【问题描述】:
我正在尝试自己学习 Spring,我计划通过创建一个博客网络应用程序来做到这一点。我已经拥有了基本的博客功能,这是一个显示博客文章的页面,以及一个带有提交表单的页面。显示博文的页面显示最新的博文,以及数据库中所有博文的标题列表。
为了从数据库中获取博客文章的有序列表,我首先在我的存储库界面中创建了一个 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:没有找到类型 int 的属性描述
在 org.springframework.data.mapping.PropertyPath.(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.data.mapping.PropertyPath.from(PropertyPath.java:271)
在 org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:245)
在 org.springframework.data.repository.query.parser.Part.(Part.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)
在 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(RepositoryFactorySupport.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();
}
我的帖子实体:
@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;
}
【问题讨论】:
-
id 应该是 Long 类型,而不是 int。
-
使用 long 而不是 int 会得到相同的结果。早些时候我也尝试对 Date 进行排序,这也给出了同样的错误。
-
你用的是long还是Long? Long 应该是可映射的。
-
我在该日期属性上缺少 @Temporal 注释。调用属性“日期”并不是一个好主意,因为这可能只是一个保留的 SQL 关键字。
-
@wxyz id 可以是原始的或原始的包装器。