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

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

Adding interval to timestamp. Producing @Query in Repository interface

问题

  1. @Query("select job from Job job, Project proj, User u, Userprops props where job.status LIKE 'CANCELED' AND job.project = proj.id AND " +
  2. "proj.user = u.id AND props.user = u.id AND NOW() < job.modifiedAt + props.removeThingsAfterDays * INTERVAL '1 day'")
  3. 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

  1. @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; +
  2. &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;)
  3. List &lt;XTMJobEntity&gt; getAllJobsThatShouldBeDeletedFromDbBasedOnUserProps ();

答案1

得分: 1

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

  1. select a job
  2. from job job
  3. , project proj
  4. , user u
  5. , userprops props
  6. where job.status = 'CANCELED'
  7. and job.project = proj.id
  8. and proj.user = u.id
  9. and props.user = u.id
  10. and now () < job.modifiedat + props.removethingsafterdays * '1 day'::interval;
  11. ---- OR
  12. select a job
  13. from job
  14. , project proj
  15. , user u
  16. , userprops props
  17. where job.status = 'CANCELED'
  18. and job.project = proj.id
  19. and proj.user = u.id
  20. and props.user = u.id
  21. and now () < job.modifiedat + props.removethingsafterdays * interval '1 day';

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

  1. @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" +
  2. "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):

  1. select a job
  2. from job job
  3. , project proj
  4. , user u
  5. , userprops props
  6. where job.status = &#39;CANCELED&#39;
  7. and job.project = proj.id
  8. and proj.user = u.id
  9. and props.user = u.id
  10. and now () &lt;job.modifiedat + props.removethingsafterdays * &#39;1 day&#39;::interval;
  11. ---- OR
  12. select a job
  13. from job
  14. , project proj
  15. , user u
  16. , userprops props
  17. where job.status = &#39;CANCELED&#39;
  18. and job.project = proj.id
  19. and proj.user = u.id
  20. and props.user = u.id
  21. 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:

  1. @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; +
  2. &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

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

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

The answer for my problem is following query:

  1. @Query(value = &quot;select job.* &quot; +
  2. &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; +
  3. &quot;INNER JOIN user_properties props ON props.user_id = u.id &quot; +
  4. &quot;where job.status = &#39;CANCELLED&#39; &quot; +
  5. &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:

确定