Spring – 我可以在 @Query 内部获取对象的ID吗?

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

Spring - Can I get the id of a object inside a @Query?

问题

我在JpaRepository中有一个查询,像这样:

@Query("SELECT p "
        + "FROM Physicians p "
        + "JOIN Physicianspecialities ps ON (ps.idphysician = p.id AND ps.idspeciality = ?1 AND ps.validfrom <= ?2 AND (ps.validto >= ?2 OR ps.validto IS NULL)) "
        + "JOIN Personnel pe ON (pe.id = p.idPersonnel and pe.validfrom <= ?2 AND (pe.validto >= ?2 OR pe.validto IS NULL)) "
        + "ORDER BY p.id DESC")
List<Physicians> getAllByDateAndSpeciality(Integer idSpeciality, Date date);

我知道这个查询是错误的,因为ps.idspeciality表示Specialities类而?1是一个整数。是否有语法允许写类似于这样的内容:"ps.idspeciality.id = ?1"?

我知道还有其他解决方法,比如:

  • 我可以创建一个Specialities对象并用它进行比较。
  • 我可以做一个额外的JOIN。
  • 或者我可以将idspeciality改成整数。

但我想知道是否不能以更简单的方式使用@Query语言来解决这个问题。事实上,我不知道在@Query注解中包含的语言文档在哪里可以找到。

英文:

I have a query in a JpaRepository like this :

@Query(&quot;SELECT p &quot;
        + &quot;FROM Physicians p &quot;
        + &quot;JOIN Physicianspecialities ps ON (ps.idphysician = p.id AND ps.idspeciality = ?1 AND ps.validfrom &lt;= ?2 AND (ps.validto &gt;= ?2 OR ps.validto IS NULL)) &quot;
        + &quot;JOIN Personnel pe ON (pe.id = p.idPersonnel and pe.validfrom &lt;= ?2 AND (pe.validto &gt;= ?2 OR pe.validto IS NULL)) &quot;
        + &quot;ORDER BY p.id DESC&quot;)
List&lt;Physicians&gt; getAllByDateAndSpeciality(Integer idSpeciality, Date date);

I know the query is wrong because ps.idspeciality represents the Specialities class and ?1 is an integer.
Is there any syntax that allows writing something like this: "ps.idspeciality.id = ?1" ?

I know that the are other sollutions like

  • I can create a Specialities object and use it for comparison.
  • I can make an extra JOIN
  • or I can make idspeciality integer

But I was wondering if I can't solve this in a more simple way using the @Query language.
Truth is I don't know where could I find the documentation for the language contained in the @Query annotation.

答案1

得分: 0

我实际上可以使用“ps.idspeciality.id”的语法在@Query中访问id属性。我遇到了一个通用错误,并错误地假设这是原因。

英文:

I can actually access the id property in the @Query using the syntax "ps.idspeciality.id" I was getting a generic error and I wrongly assumed this was the cause.

huangapple
  • 本文由 发表于 2023年7月31日 19:37:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803237.html
匿名

发表评论

匿名网友

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

确定