英文:
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("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);
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论