添加时间间隔到时间戳。在存储库接口中生成 @Query。

huangapple go评论79阅读模式
英文:

Adding interval to timestamp. Producing @Query in Repository interface

问题

@Query("select job from Job job, Project proj, User u, Userprops props where job.status LIKE 'CANCELED' AND job.project = proj.id AND " +
       "proj.user = u.id AND props.user = u.id AND NOW() < job.modifiedAt + props.removeThingsAfterDays * INTERVAL '1 day'")
List<XTMJobEntity> getAllJobsThatShouldBeDeletedFromDbBasedOnUserProps();
英文:

Database: Postgres,
Programming Technology: SpringBoot (Java 11)

I am trying to create @Query but the way I implemented it Hibernate won't produce it and I get the exception that the problem is the interval function.
I want to add some days to a modifiedAt column depending on UserProps and then decide whether Db should select it or not. I call the Postgres INTERVAL internal function but it doesn't seem to work

  @Query (&quot;select a job from Job job, Project proj, User u, Userprops props, where job.status LIKE &#39;CANCELED&#39; AND job.project = proj.id AND \ n&quot; +
             &quot;proj.user = u.id AND props.user = u.id AND NOW () &lt;job.modifiedAt + props.removeThingsAfterDays * FUNCTION (&#39;INTERVAL&#39; &#39;1 day&#39;)&quot;)
     List &lt;XTMJobEntity&gt; getAllJobsThatShouldBeDeletedFromDbBasedOnUserProps ();

答案1

得分: 1

在Postgres中,interval 不是一个函数,而是一种数据类型。即使它是一个函数,你仍然不会将FUNCTION用作关键字。在Postgres中,查询将如下(仍然使用过时的连接语法):

select a job 
  from job job
     , project proj
     , user u
     , userprops props
 where job.status  = 'CANCELED' 
   and job.project = proj.id 
   and proj.user   = u.id 
   and props.user  = u.id 
   and now () < job.modifiedat + props.removethingsafterdays * '1 day'::interval;
   
---- OR 

select a job                                                                    
   from job                                                                     
      , project proj                                                            
      , user u                                                                  
      , userprops props                                                         
  where job.status  = 'CANCELED'                                                
    and job.project = proj.id                                                   
    and proj.user   = u.id                                                      
    and props.user  = u.id                                                      
    and now () < job.modifiedat + props.removethingsafterdays * interval '1 day';

注意:在SQL语句中,如果没有通配符(%或_),LIKE与等号(=)等效。<br/>
<br/>
所以也许你的查询字符串变成了:

@Query("select a job from Job job, Project proj, User u, Userprops props, where job.status LIKE 'CANCELED' AND job.project = proj.id AND \ n" +
                 "proj.user = u.id AND props.user = u.id AND NOW () < job.modifiedAt + props.removeThingsAfterDays * INTERVAL '1 day'")
英文:

In Postgres interval is not a function, but a data type. Even if it were a though you still would not use FUNCTION as a key word. In Postgres the query would be (leaving the obsolete join syntax in place):

select a job 
  from job job
     , project proj
     , user u
     , userprops props
 where job.status  = &#39;CANCELED&#39; 
   and job.project = proj.id 
   and proj.user   = u.id 
   and props.user  = u.id 
   and now () &lt;job.modifiedat + props.removethingsafterdays * &#39;1 day&#39;::interval;

---- OR 
    
select a job                                                                    
   from job                                                                     
      , project proj                                                            
      , user u                                                                  
      , userprops props                                                         
  where job.status  = &#39;CANCELED&#39;                                                
    and job.project = proj.id                                                   
    and proj.user   = u.id                                                      
    and props.user  = u.id                                                      
    and now () &lt;job.modifiedat + props.removethingsafterdays * interval &#39;1 day&#39;;

Note: LIKE in a SQL statement without wild cards (% or _) is the equivalent of equal operator (=). <br/>
<br/>
So perhaps your query string becomes:

  @Query (&quot;select a job from Job job, Project proj, User u, Userprops props, where job.status LIKE &#39;CANCELED&#39; AND job.project = proj.id AND \ n&quot; +
             &quot;proj.user = u.id AND props.user = u.id AND NOW () &lt;job.modifiedAt + props.removeThingsAfterDays * INTERVAL &#39;1 day&#39;&quot;)

答案2

得分: 0

我问题的答案是以下查询:

@Query(value = "select job.* " +
       "from jobs job INNER JOIN projects proj ON job.project_id = proj.id INNER JOIN users u ON proj.user_id = u.id " +
       "INNER JOIN user_properties props ON props.user_id = u.id " +
       "where job.status = 'CANCELLED' " +
       "AND NOW() < job.modified_at + props.remove_cancelled_jobs_days * Interval '1days';", nativeQuery = true)
英文:

The answer for my problem is following query:

@Query(value = &quot;select job.* &quot; +
       &quot;from jobs job INNER JOIN projects proj ON job.project_id = proj.id INNER JOIN users u ON proj.user_id = u.id &quot; +
       &quot;INNER JOIN user_properties props ON props.user_id = u.id &quot; +
       &quot;where job.status = &#39;CANCELLED&#39; &quot; +
       &quot;AND NOW() &lt; job.modified_at + props.remove_cancelled_jobs_days * Interval &#39;1days&#39;;&quot;, nativeQuery = true)

huangapple
  • 本文由 发表于 2020年8月28日 19:44:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63633145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定