如何在spring启动时只使用hql获取一条记录 [英] How to get only one record using hql in spring boot

查看:227
本文介绍了如何在spring启动时只使用hql获取一条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在春季开机时使用了休眠功能。我想通过传递一个id从表中获取下一个和上一个记录。我在hql中写了一个查询来获取表中的下一个和前一个记录。问题是我收到所有以前和所有下一个记录,但我只想要一个前一个和最后一个记录。



这是我的 DAO 下一条记录的类

  @Transactional 
public interface DataDao extends CrudRepository< Data,Long> {

@Query(getDataId)
NextPrevData getDataId(Long post_id,Long data_id);

final String getDataId =从数据d中选择新的netgloo.models.NextPrevData(d.id),其中d.postId =?1和d.id>?2;

$ b

以前是 DAO 记录

  @Transactional 
public interface PrevDao extends CrudRepository< Data,Long> {

@Query(getDataId)
NextPrevData getDataId(Long post_id,Long data_id);

final String getDataId =从Data d中选择新的netgloo.models.NextPrevData(d.id),其中d.postId =?1和d.id <?2;




这是我的控制器

  @RequestMapping(/ {post_id} / {data_id})
@ResponseBody
public Next getDataInfo (@PathVariable(post_id)long post_id,@PathVariable(data_id)long data_id,HttpServletRequest req,HttpServletResponse res)throws ServletException {
NextPrevData Next = dataDao.getDataId(post_id,data_id);
NextPrevData Prev = prevDao.getDataId(post_id,data_id);
Post2 postobj = postDao2.findOne(post_id);
Data d = dataDao.findOne(data_id);
if(Next.isEquals(null)){
postobj.setNextStatus(false);
}
else {
postobj.setNextStatus(true);


if(Prev.isEmpty()){
postobj.setPrevStatus(false);
}
else {
postobj.setPrevStatus(true);
}

返回新的下一个(Next,postobj,Prev,d);
}

这是我的下一课课程

  public class Next {

private NextAndPrevious Next;
私人NextAndPrevious上一页;
私人Post2帖子;
私人数据d;

public Post2 getPost(){
return post;
}

public void setPost(Post2 post){
this.post = post;
}

public Next(NextAndPrevious Next,Post2 postobj,NextAndPrevious Prev,Data d){
this.Next = Next;
this.post = postobj;
this.Prev = Prev;
this.d = d;

// Getters and seters


解决方案

  @Transactional 
public interface DataDao extends CrudRepository< Data,Long> {

@Query(getDataId)
List< NextPrevData> getDataId(长post_id,长data_id);

final String getDataId =从数据d中选择新的netgloo.models.NextPrevData(d.id),其中d.postId =?1和d.id>?2;


$ / code>

这是您以前的记录

  @Transactional 
public interface PrevDao extends CrudRepository< Data,Long> {

@Query(getDataId)
List< NextPrevData> getDataId(长post_id,长data_id);

final String getDataId =从Data d中选择新的netgloo.models.NextPrevData(d.id),其中d.postId =?1和d.id <?2;

$ b $ / code>

您的控制器

  RequestMapping(/ {post_id} / {data_id})
@ResponseBody
public Next getDataInfo(@PathVariable(post_id)long post_id ,@PathVariable(data_id)long data_id,HttpServletRequest req,HttpServletResponse res)throws ServletException {



List< NextPrevData> Next = dataDao.getDataId(post_id,data_id);
List< NextPrevData> Prev = prevDao.getDataId(post_id,data_id);
Post2 postobj = postDao2.findOne(post_id);
Data d = dataDao.findOne(data_id);
长a = 0;
long b = 0;
if(Next.isEmpty()){
postobj.setNextStatus(false);
}
else {
postobj.setNextStatus(true);
a = Next.get(0).getDataId();


if(Prev.isEmpty()){
postobj.setPrevStatus(false);
}
else {
postobj.setPrevStatus(true);
b = Prev.get(0).getDataId();
}

return new下一个(a,postobj,b,d);
}

以下是您的下一课

  public class Next {

private long Next;
私人长期上一页;
私人Post2帖子;
私人数据d;

public Post2 getPost(){
return post;
}

public void setPost(Post2 post){
this.post = post;

$ b $ public下一个(long Next,Post2 postobj,long Prev,Data d){
this.Next = Next;
this.post = postobj;
this.Prev = Prev;
this.d = d;
}
// Getters and setters
}


I am using hibernate with spring boot. I want to get next and previous record from table by passing an id. I have written a query in hql to get next and previous records from table. The problem is that i am getting all previous and all next records but i want only one previous and one last record.

Here is my DAO class for next record

@Transactional
public interface DataDao extends CrudRepository<Data, Long> {

@Query(getDataId)
NextPrevData getDataId(Long post_id, Long data_id);

final String getDataId= "select new netgloo.models.NextPrevData(d.id) from Data d where d.postId=?1 and d.id>?2";

}

Here is DAO for previous record

@Transactional
public interface PrevDao extends CrudRepository<Data, Long> {

@Query(getDataId)
NextPrevData getDataId(Long post_id, Long data_id);

final String getDataId= "select new netgloo.models.NextPrevData(d.id) from Data d where d.postId=?1 and d.id<?2";

}

Here is my Controller

@RequestMapping("/{post_id}/{data_id}")
@ResponseBody 
public Next getDataInfo(@PathVariable("post_id") long post_id,       @PathVariable("data_id") long data_id, HttpServletRequest req, HttpServletResponse res) throws ServletException {
NextPrevData Next = dataDao.getDataId(post_id, data_id);
NextPrevData Prev = prevDao.getDataId(post_id, data_id);
Post2 postobj = postDao2.findOne(post_id);
Data d = dataDao.findOne(data_id);
if(Next.isEquals(null)){
postobj.setNextStatus(false);
}
else{
postobj.setNextStatus(true);
}

if(Prev.isEmpty()){
postobj.setPrevStatus(false);
}
else{
postobj.setPrevStatus(true);
}

return new Next(Next,postobj,Prev,d);
}

Here is myNext class

public class Next {

private NextAndPrevious Next;
private NextAndPrevious Prev;
private Post2 post;
private Data d;

public Post2 getPost() {
    return post;
}

public void setPost(Post2 post) {
    this.post = post;
}

public Next(NextAndPrevious Next, Post2 postobj, NextAndPrevious Prev, Data d) {
    this.Next = Next;
    this.post = postobj;
    this.Prev = Prev;
    this.d = d;
}
//Getters and seters

解决方案

Here is your DAO for next record

@Transactional
public interface DataDao extends CrudRepository<Data, Long> {

@Query(getDataId)
List<NextPrevData> getDataId(Long post_id, Long data_id);

final String getDataId= "select new netgloo.models.NextPrevData(d.id) from Data d where d.postId=?1 and d.id>?2";

}

Here is your DAO for previous record

@Transactional
public interface PrevDao extends CrudRepository<Data, Long> {

@Query(getDataId)
List<NextPrevData> getDataId(Long post_id, Long data_id);

final String getDataId= "select new netgloo.models.NextPrevData(d.id) from Data d where d.postId=?1 and d.id<?2";

}

Your Controller

RequestMapping("/{post_id}/{data_id}")
@ResponseBody 
public Next getDataInfo(@PathVariable("post_id") long post_id,  @PathVariable("data_id") long data_id, HttpServletRequest req, HttpServletResponse res) throws ServletException {



List<NextPrevData> Next = dataDao.getDataId(post_id, data_id);
List<NextPrevData> Prev = prevDao.getDataId(post_id, data_id);
Post2 postobj = postDao2.findOne(post_id);
Data d = dataDao.findOne(data_id);
long a=0;
long b=0;
if(Next.isEmpty()){
postobj.setNextStatus(false);
}
else{
postobj.setNextStatus(true);
a = Next.get(0).getDataId();
}

if(Prev.isEmpty()){
postobj.setPrevStatus(false);
}
else{
postobj.setPrevStatus(true);
b = Prev.get(0).getDataId();
}

return new Next(a,postobj,b,d);
}

And here is your Next class

public class Next {

private long Next;
private long Prev;
private Post2 post;
private Data d;

public Post2 getPost() {
    return post;
}

public void setPost(Post2 post) {
    this.post = post;
}

public Next(long Next, Post2 postobj, long Prev, Data d) {
    this.Next = Next;
    this.post = postobj;
    this.Prev = Prev;
    this.d = d;
}
//Getters and setters
}

这篇关于如何在spring启动时只使用hql获取一条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆