大家好,我都使用spring谓词对实体进行多次搜索,我目前需要为特定医生使用谓词,是否有一种方法可以为jpq查询方法发送谓词
请不要说,如果有一种使用谓词来传递关系的方法(例如,通过医生ID),它将更加容易
我可以在findAllByDoctor上传递谓词对象吗?
Page<Expenses> expenses = expensesRepository.findAllByDoctor(doctor,predicate,pageable);
请注意,如果我使用以下代码,它将起作用
Page<Expenses> expenses = expensesRepository.findAll(predicate,pageable);
在日志中发现以下错误
20:19:53.106 [http-nio-8531-exec-5] TRACE o.h.r.j.i.ResourceRegistryStandardImpl - Releasing JDBC resources
at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:100)
20:19:53.106 [http-nio-8531-exec-5] TRACE o.h.r.j.i.AbstractLogicalConnectionImplementor - re-enabling auto-commit on JDBC Connection after completion of JDBC-based transaction
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$CountQueryPreparer.invokeBinding(PartTreeJpaQuery.java:270)
20:19:53.106 [http-nio-8531-exec-5] TRACE o.h.r.t.b.j.i.JdbcResourceLocalTransactionCoordinatorImpl - ResourceLocalTransactionCoordinatorImpl#afterCompletionCallback(false)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.createQuery(PartTreeJpaQuery.java:142)
20:19:53.106 [http-nio-8531-exec-5] TRACE o.h.r.t.i.SynchronizationRegistryStandardImpl - SynchronizationRegistryStandardImpl.notifySynchronizationsAfterTransactionCompletion(5)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.doCreateCountQuery(PartTreeJpaQuery.java:88)
20:19:53.106 [http-nio-8531-exec-5] TRACE o.h.r.j.i.AbstractLogicalConnectionImplementor - LogicalConnection#afterTransaction
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.doCreateCountQuery(PartTreeJpaQuery.java:40)
20:19:53.106 [http-nio-8531-exec-5] TRACE o.h.r.j.i.ResourceRegistryStandardImpl - Releasing JDBC resources
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.createCountQuery(AbstractJpaQuery.java:215)
20:19:53.107 [http-nio-8531-exec-5] DEBUG o.h.r.j.i.LogicalConnectionManagedImpl - Initiating JDBC connection release from afterTransaction
at org.springframework.data.jpa.repository.query.JpaQueryExecution$PagedExecution.doExecute(JpaQueryExecution.java:181)
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:82)
20:19:53.107 [http-nio-8531-exec-5] TRACE o.h.r.j.i.ResourceRegistryStandardImpl - Releasing JDBC resources
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:114)
20:19:53.107 [http-nio-8531-exec-5] TRACE org.hibernate.internal.SessionImpl - SessionImpl#afterTransactionCompletion(successful=false, delayed=false)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:104)
20:19:53.107 [http-nio-8531-exec-5] TRACE o.s.orm.jpa.JpaTransactionManager - Triggering afterCompletion synchronization
20:19:53.107 [http-nio-8531-exec-5] TRACE o.s.t.s.TransactionSynchronizationManager - Clearing transaction synchronization
实体类
package com.osc.generateddataobjects;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.querydsl.core.annotations.QueryEntity;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.*;
import java.util.Date;
@QueryEntity
@Entity
@Table(name="EXPENSES")
@Getter
@Setter
public class Expenses {
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long expensesId;
@Column(name = "DESCRIPTION")
private String description;
@Column(name = "TITLE")
private String title;
@Column(name = "PURPOSE")
private String purpose ;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
@Column(name = "EXPENSES_DATE")
private Date date ;
@Column(name = "AMOUNT")
private Double amount ;
@ManyToOne
@JoinColumn(name = "DOCTOR_ID")
@JsonIgnore
private Doctor doctor;
}
存储库类
package com.osc.infra.repositories;
import com.osc.generateddataobjects.Doctor;
import com.osc.generateddataobjects.Expenses;
import com.osc.generateddataobjects.QExpenses;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
import org.springframework.data.querydsl.binding.QuerydslBindings;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import com.querydsl.core.types.Predicate;
public interface ExpensesRepository extends PagingAndSortingRepository<Expenses, Long> ,
QueryDslPredicateExecutor<Expenses>, QuerydslBinderCustomizer<QExpenses> {
@Override
default void customize(QuerydslBindings bindings, QExpenses expenses) {
//change based on request
bindings.bind(expenses.date).all((path, value) -> {
Iterator<? extends Date> it = value.iterator();
return path.between(it.next(), it.next());
});
bindings.bind(expenses.doctor).first(( path,value) -> path.eq(value));
bindings.bind(expenses.title).first(( path,value) -> path.containsIgnoreCase(value));
bindings.bind(expenses.amount).first(( path,value) -> path.eq(value));
bindings.bind(expenses.description).first(( path,value) -> path.containsIgnoreCase(value));
bindings.bind(expenses.purpose).first(( path,value) -> path.containsIgnoreCase(value));
}
public Page<Expenses> findAllByDoctor(Doctor doctor, Predicate predicate, Pageable pageable);
}
RestController
@GetMapping(value = "/resources/expenses/doctor/{logonId}")
public ResponseEntity<GetListResponse> getAllExpensesForDoctor(Pageable pageable, @QuerydslPredicate(root = Expenses.class) Predicate predicate,OAuth2Authentication auth,@PathVariable("logonId") String logonId){
GetListResponse expenses = expensesOperations.getAllExpensesByDoctor(logonId,predicate,pageable);
return new ResponseEntity<>(expenses,HttpStatus.OK);
}