gpt4 book ai didi

java - 如何使用 Spring Data JPA 执行存储为字符串的查询?

转载 作者:行者123 更新时间:2023-11-30 06:47:03 27 4
gpt4 key购买 nike

我正在以编程方式将 SQL 查询构造为字符串,并希望将其作为 SQL 语句执行,但我使用的是 Spring Data JPA,并且无法将 JDBC 与准备好的语句一起使用。这在 JPA 中可能吗?

public class Polygon {

public static void main(String[] args) {

// Build Lat/Longs

Set<String[]> coordinates = new HashSet<>();

coordinates.add(new String[]{"42", "-72.95333862304689"});
coordinates.add(new String[]{"42.05337156043361", "-71.06643676757814"});
coordinates.add(new String[]{"41.32732632036624", "-71.06643676757814"});
coordinates.add(new String[]{"41.32732632036624", "-72.95333862304689"});
coordinates.add(new String[]{"42.05337156043361", "-72"});

// Trim & Build Query

String query = "SELECT * FROM SITE WHERE (LATITUDE LIKE ";

for(String[] c : coordinates){

c[0] += ".0";
c[1] += ".0";

String[] latArray = c[0].split("\\.");
String[] lngArray = c[1].split("\\.");

String lat = latArray[0] + "." + latArray[1].substring(0, 1) + "%";
String lng = lngArray[0] + "." + lngArray[1].substring(0, 1) + "%";

query += lat + " AND LONGITUDE LIKE " + lng + ") OR (LATITUDE LIKE ";
}

query = query.substring(0, query.length() - 20) + ");";
System.err.println(query);

/*
* This is the Query I am printing, which is correct
*
* SELECT * FROM SITE
* WHERE (LATITUDE LIKE 41.3% AND LONGITUDE LIKE -71.0%)
* OR (LATITUDE LIKE 42.0% AND LONGITUDE LIKE -72.0%)
* OR (LATITUDE LIKE 42.0% AND LONGITUDE LIKE -72.9%)
* OR (LATITUDE LIKE 42.0% AND LONGITUDE LIKE -71.0%)
* OR (LATITUDE LIKE 41.3% AND LONGITUDE LIKE -72.9%);
*
* How to execute this with Spring Data JPA?
*/

// @Query("SELECT * FROM SITE :where", nativeQuery = true)
// public List<SiteDTO> executeSiteQuery(@Param("where") String where);
// Doesn't Work, because (Syntax error on token ""SELECT * FROM SITE :where"", invalid MemberValuePairs)
}
}

最佳答案

您的查询看起来太动态,无法被 Spring Data JPA 注释理解。事实上,当您尝试将 SQL 注入(inject)参数字段时,您尝试的解决方法将无效,因此框架将阻止您这样做以防止 SQL 注入(inject)。

您可以将自定义行为添加到存储库并实现准备好的语句(请参阅如何从 EntityManager here 获取 session )。基本上:

public class MyRepositoryImpl<SiteDTO, ID extends Serializable>
extends SimpleJpaRepository<SiteDTO, ID> implements MyRepository<SiteDTO, ID> {

private EntityManager entityManager;

// There are two constructors to choose from, either can be used.
public MyRepositoryImpl(Class<T> domainClass, EntityManager entityManager) {
super(domainClass, entityManager);

// This is the recommended method for accessing inherited class dependencies.
this.entityManager = entityManager;
}

public List<SiteDTO> executeSiteQuery(List<String> longitudesToMatch, List<String> latitudesToMatch) {
// Here you can grab the session from the entity manager and create your own query
}
}

否则,如果您认为它太过分了,您可以考虑执行or Spring 服务中查询的一部分。我的意思是,像这样的查询 public List<SiteDTO> executeSiteQuery(@Param("latitude") String latitude, @Param("longitude") String longitude);并多次调用它。性能较差,但有时简单会带来返回。

关于java - 如何使用 Spring Data JPA 执行存储为字符串的查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43524569/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com