有没有办法在 querydsl 中执行此查询?
SELECT *
FROM table
WHERE replace(column_name, ' ', '') = 'someValue';
Run Code Online (Sandbox Code Playgroud)
从StringPath没有QClass。replace()函数,并且在使用 测试之前有必要删除一些字符(特别是中间的空格) 。column_namesomeValue
样本column_name内容:ABC, DEF, AB *
如果someValue是ABC,ABC和AB*应该出现。
我有一个带有代表上次活动的 Date 对象的实体。我想查询平均空闲时间。所以在 SQL 中类似:
SELECT AVG(NOW() - idle_date) FROM mytable WHERE ....
Run Code Online (Sandbox Code Playgroud)
我尝试这样计算差异:
query.singleResult(
Expressions.dateOperation(
Long.class,
Ops.DateTimeOps.DIFF_SECONDS,
DateExpression.currentDate(),
myTable.idleDate
)
);
Run Code Online (Sandbox Code Playgroud)
但这不允许我对结果求平均值,只能对最小值和最大值求平均值。
如何用日期/时间差(以秒为单位)表达该平均值?
我想使用 QueryDSL 库构建选择计数查询,如下所示:
select count(1) from table1
我创建了下一个代码:
SQLTemplates sqlTemplates = builder.printSchema().build();
Configuration configuration = new Configuration(sqlTemplates);
Path table = new RelationalPathBase(Object.class, "alias", "hr", "table1");
StringPath[] columnsPath = new StringPath[1];
columnsPath[0] = Expressions.stringPath(table, "field1");
SQLQuery sqlQuery = new SQLQuery(dataSource.getConnection(), configuration)
.from(table);
sqlQuery.setUseLiterals(true);
String selectStatement = sqlQuery.getSQL(columnsPath).getSQL();
Run Code Online (Sandbox Code Playgroud)
结果 selectStatement 是下一个:
select alias.field1 from hr.table1 alias
Run Code Online (Sandbox Code Playgroud)
请有人建议如何重写上面的代码以获得
select count(alias.field1) from hr.table1 alias
Run Code Online (Sandbox Code Playgroud) 有什么办法可以用 QueryDSL 获取这个查询吗?
select
person.name,
count(neighbors.*)
from person as neighbors
where person.address = neighbor.address
group by person.name
Run Code Online (Sandbox Code Playgroud)
其中address不是 FK。
我有一个像这样搜索的控制器方法
@RequestMapping(method = RequestMethod.GET)
public Page<MyEntity> find(@QuerydslPredicate(root = MyEntity.class)
Predicate predicate,
Pageable pageable) {
return this.myEntityRepository.findAll(predicate, pageable);
}
Run Code Online (Sandbox Code Playgroud)
效果很好。我可以发出GET带有各种查询字符串参数的请求,并进行相应的过滤,但现在我想按 null 进行搜索。我尝试做类似的事情/myentity?myParam1=&,但predicate争论总是如此null。
如何搜索特定字段为空的位置?
我刚刚将 spring boot 版本更新到1.4.0。之后我收到类似的错误
Error:(109, 45) java: no suitable method found for findAll(com.mysema.query.types.Predicate,org.springframework.data.domain.Pageable)
method org.springframework.data.querydsl.QueryDslPredicateExecutor.findAll(com.querydsl.core.types.Predicate,org.springframework.data.domain.Sort) is not applicable
(argument mismatch; com.mysema.query.types.Predicate cannot be converted to com.querydsl.core.types.Predicate)
method org.springframework.data.querydsl.QueryDslPredicateExecutor.findAll(com.querydsl.core.types.Predicate,com.querydsl.core.types.OrderSpecifier<?>...) is not applicable
(argument mismatch; com.mysema.query.types.Predicate cannot be converted to com.querydsl.core.types.Predicate)
method org.springframework.data.querydsl.QueryDslPredicateExecutor.findAll(com.querydsl.core.types.OrderSpecifier<?>...) is not applicable
(varargs mismatch; com.mysema.query.types.Predicate cannot be converted to com.querydsl.core.types.OrderSpecifier<?>)
method org.springframework.data.querydsl.QueryDslPredicateExecutor.findAll(com.querydsl.core.types.Predicate,org.springframework.data.domain.Pageable) is not applicable
(argument mismatch; com.mysema.query.types.Predicate cannot be converted to com.querydsl.core.types.Predicate)
Run Code Online (Sandbox Code Playgroud)
升级之前,查询工作正常。
我正在尝试在我的项目中使用 QDataTableRepository 。这让我必须导入querydsl。
构建.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'idea'
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-devtools")
compile('org.springframework.boot:spring-boot-starter')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.postgresql:postgresql:9.4.1212')
compile('org.projectlombok:lombok:1.16.10')
compile group: 'com.github.darrachequesne', name: 'spring-data-jpa-datatables', version: '3.1'
compile group: 'com.querydsl', name: 'querydsl-jpa', version: '4.1.4'
compile group: 'com.querydsl', name: 'querydsl-apt', version: '4.1.4'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
configurations {
querydslapt
}
sourceSets {
generated
}
sourceSets.generated.java.srcDirs = ['generated/']
task generateQueryDSL(type: JavaCompile, group: 'build', description: 'Generates the QueryDSL query types') {
source = sourceSets.main.java
classpath = configurations.compile …Run Code Online (Sandbox Code Playgroud) 我使用带有 Spring REST 端点的 QueryDSL 谓词对象来检索和查询参数值。
@GetMapping("/{subjectId}/students")
@RolesAllowed( {Roles.PLATFORM_ADMIN, Roles.USER})
public List<StudentResponse> getAllStudents(@PathVariable final String subjectId,
@QuerydslPredicate(root = Student.class) final Predicate predicate) {
final Predicate searchPredicate = studentPredicate()
.predicate(predicate)
.subjectId(subjectId)
.build();
return studentService.findBySubjectId(subjectId, searchPredicate);
}
Run Code Online (Sandbox Code Playgroud)
Student类包含studentId和studentName属性;
现在,如果有人调用https://hostname/ {subjectId}/students?studentId=1234&studentName=test
然后上面的代码生成带有参数值的谓词对象。但是我需要从谓词对象中获取以上 2 个参数值,以便除了数据库查询之外进行进一步处理。我没有看到谓词对象有任何支持方法来检索值。那么我该怎么做呢?
我想使用org.springframework.data.domain.Pageablewithcom.querydsl.jpa.impl.JPAQuery有没有办法或者有可靠的解决方法如何使 Pageable 与 JPAQuery 一起工作?
我正在考虑使用 DTO 投影 - 我有两个具有一对多关系的实体(EntityOne 的一个实例链接到 EntityTwo 的多个实例),并且我想将结果作为新的 DTO 对象返回 - 我的内容我目前正在尝试的是这样的:
query.select(Projections.constructor(MyDtoObject.class,
entityOne, list(entityTwo)))
.from(entityOne, entityTwo)
.where(......)
Run Code Online (Sandbox Code Playgroud)
MyDtoObject 看起来像这样:
public class MyDtoObject {
private EntityOne entityOne;
private Collection<EntityTwo> entityTwoCollection
// getters, setters and an all args constructor method here
}
Run Code Online (Sandbox Code Playgroud)
然而,这会带回比预期多得多的 MyDtoObjects,而且看起来每个 MyDtoObjects 只保存一个entityTwo 对象而不是一个集合。
如何指示 queryDSL 创建具有多个entityTwo 条目的 MyDtoObjects 结果对象?list(..) 方法在我上面的场景中是否执行任何操作?