我正在尝试自己学习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() { …Run Code Online (Sandbox Code Playgroud) 我的应用程序是一个简单的CRUD应用程序 我有一个删除控制器操作,当项目成功删除时,它会重定向回列表.我现在要添加的是向用户发出的一条消息,即数据已成功删除.
我的控制器动作:
public function deleteItem($id)
{
try {
$item = Item::findOrFail($id);
$item->delete();
return Redirect::to('list')->with('message', 'Successfully deleted');
} catch (ModelNotFoundException $e) {
return View::make('errors.missing');
}
}
Run Code Online (Sandbox Code Playgroud)
我的list.blade.php视图中我尝试显示消息的部分:
@if (isset($message))
<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
{{ $message }}
</div>
@endif
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是$message变量总是空的..
我有以下情况:
onPause,onSaveInstanceState和onStop方法.onDestroy),然后重新创建.(这似乎就是这样发生的.我没有实现这种方式,似乎只是Android的做事方式..)onCreate方法期间,变量savedInstanceState始终为null.我知道这里有类似的话题,但他们似乎都没有对我的情况有答案.所有的回调方法都有日志行,所以我确定执行了save方法并执行了destory.但为什么从来没有一个savedInstanceState对象呢?
我的保存方法:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putSerializable(OpenSessionsActivity.SESSION, session);
System.out.println("saving ..");
super.onSaveInstanceState(savedInstanceState);
}
Run Code Online (Sandbox Code Playgroud)
你还需要我包含其他代码吗?