我很难在 JPA 中的不相关域之间加入
I am having a hard time joining between unrelated domains in JPA
我在学习JPA的过程中遇到了各种各样的事情,但对它太陌生了,所以想得到一些建议。
我在学习过程中卡住的部分分为三大类。你能看看下面的代码吗?
@Repository
public interface TestRepository extends JpaRepository<TestEntity,Long> {
@Query(" SELECT
, A.test1
, A.test2
, B.test1
, B.test2
FROM TEST_TABLE1 A
LEFT JOIN TEST_TABLE2 B
ON A.test_no = B.test_no
WHERE A.test3 = ?1 # Here's the first question
if(VO.test4 is not null) AND B.test4 = ?2") # Here's the second question
List<Object[] # Here's the third question> getTestList(VO);
}
首先,是否可以从使用原生sql收到的VO中提取test3?
String test1一般都是这样使用的,不知道有没有其他的方法
其次,如果在VO中可以提取,是否可以根据Test4是否赋值在@QUERY中添加查询?
第三,如果我使用 List
First, is it possible to extract test3 from the VO received when using native sql? Usually, String test1 is used like this, but I wonder if there is any other way other than this.
是的,有可能。
您必须使用,例如 :#{[0].test3}
等于 vo.test3
[0] 是第一个参数的位置,用于使用 @Query
注释的方法
@Query(value = "SELECT a.test1, a.test2, b.test1, b.test2
FROM test_table1 a
LEFT JOIN test_table2 b ON a.test_no = b.test_no
WHERE a.test3 = :#{[0].test3}", nativeQuery = true)
List<Object[]> getList(VO);
Second, if extracting is possible in VO, can you add a query in @QUERY depending on whether Test4 is valued or not?
你可以使用一个技巧,例如:
SELECT ... FROM table a
LEFT JOIN table b ON a.id = b.id
WHERE a.test3 = :#{[0].test3}
AND (:#{[0].test4} IS NOT NULL AND b.test4 = :#{[0].test4})
Thirdly, if I use List<Object[]>, can the result of executing a query that is not in the already created entity (eg, test1 in TEST_TABLE2, which is not in the entity of TEST_TABLE1) can be included?
抱歉,第三个问题我没看懂
也许本教程会对您有所帮助:https://www.baeldung.com/jpa-queries-custom-result-with-aggregation-functions
我在学习JPA的过程中遇到了各种各样的事情,但对它太陌生了,所以想得到一些建议。
我在学习过程中卡住的部分分为三大类。你能看看下面的代码吗?
@Repository
public interface TestRepository extends JpaRepository<TestEntity,Long> {
@Query(" SELECT
, A.test1
, A.test2
, B.test1
, B.test2
FROM TEST_TABLE1 A
LEFT JOIN TEST_TABLE2 B
ON A.test_no = B.test_no
WHERE A.test3 = ?1 # Here's the first question
if(VO.test4 is not null) AND B.test4 = ?2") # Here's the second question
List<Object[] # Here's the third question> getTestList(VO);
}
首先,是否可以从使用原生sql收到的VO中提取test3?
String test1一般都是这样使用的,不知道有没有其他的方法
其次,如果在VO中可以提取,是否可以根据Test4是否赋值在@QUERY中添加查询?
第三,如果我使用 List
First, is it possible to extract test3 from the VO received when using native sql? Usually, String test1 is used like this, but I wonder if there is any other way other than this.
是的,有可能。
您必须使用,例如 :#{[0].test3}
等于 vo.test3
[0] 是第一个参数的位置,用于使用 @Query
@Query(value = "SELECT a.test1, a.test2, b.test1, b.test2
FROM test_table1 a
LEFT JOIN test_table2 b ON a.test_no = b.test_no
WHERE a.test3 = :#{[0].test3}", nativeQuery = true)
List<Object[]> getList(VO);
Second, if extracting is possible in VO, can you add a query in @QUERY depending on whether Test4 is valued or not?
你可以使用一个技巧,例如:
SELECT ... FROM table a
LEFT JOIN table b ON a.id = b.id
WHERE a.test3 = :#{[0].test3}
AND (:#{[0].test4} IS NOT NULL AND b.test4 = :#{[0].test4})
Thirdly, if I use List<Object[]>, can the result of executing a query that is not in the already created entity (eg, test1 in TEST_TABLE2, which is not in the entity of TEST_TABLE1) can be included?
抱歉,第三个问题我没看懂
也许本教程会对您有所帮助:https://www.baeldung.com/jpa-queries-custom-result-with-aggregation-functions