我正在构建一个 SpringData/QueryDSL 基础存储库。我有以下一组课程:
@Entity
@Table(name="t_ows_jo")
@NamedEntityGraphs({
@NamedEntityGraph(
name="graph.Jo",
attributeNodes={
...
},
subgraphs={
...
}
)
})
public class Jo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
protected Long id;
private String joNo;
private String vin;
private Date dateFiled;
private Integer mileage;
private Vehicle vehicle;
private Dealer dealer;
private List<JoJobRequest> jobRequest;
private List<JoJobDone> jobDone;
...
}
@NoRepositoryBean
public interface BaseRepository<Entity, ID extends Serializable> extends JpaRepository<Entity, ID>, QueryDslPredicateExecutor<Entity> {
Entity retrieve(Map<String, Object> filters);
... more custom methods here
List<Entity> listAll(Map<String, Object> qryParam, int …
Run Code Online (Sandbox Code Playgroud) 如何使用 Predicate BooleanExpression 对多对多关系进行内连接?
我有 2 个实体
public class A {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@ManyToMany(fetch = FetchType.LAZY,
cascade = { CascadeType.DETACH, CascadeType.MERGE,
CascadeType.REFRESH, CascadeType.PERSIST})
@JoinTable(name = "a_b_maps",
joinColumns = @JoinColumn(name = "a_id", nullable =
false,referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "b_id", nullable = false,
referencedColumnName = "id")
)
private Set<B> listOfB = new HashSet<B>();
}
public class B {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@ManyToMany(fetch = FetchType.LAZY,
cascade = { CascadeType.DETACH, CascadeType.MERGE,
CascadeType.REFRESH, …
Run Code Online (Sandbox Code Playgroud) 我正在学习junit
使用 Spring Boot 进行测试,我正在尝试为课堂编写测试用例AccountController
。帐户控制器依赖于帐户服务。我用的 mockito
是那个。但我收到错误,因为与模拟的交互为零
账户控制器
@GetMapping("/findAccountData")
public ResponseEntity<List<Tuple>> populateGridViews(@RequestParam(value="sClientAcctId",required=false) String sClientAcctId,
@RequestParam(value="sAcctDesc",required=false) String sAcctDesc,
@RequestParam(value="sInvestigatorName",required=false)String sInvestigatorName,
@RequestParam(value="sClientDeptId",required=false) String sClientDeptId) throws Exception {
return ResponseEntity.ok(accService.populateGridViews(sClientAcctId, sAcctDesc,sInvestigatorName,sClientDeptId));
}
Run Code Online (Sandbox Code Playgroud)
账户服务
public List<Tuple> populateGridViews(String sClientAcctId, String sAcctDesc, String sInvestigatorName,
String sClientDeptId)throws Exception{
QAccount account = QAccount.account;
QDepartment department = QDepartment.department;
QAccountCPCMapping accountCPCMapping = QAccountCPCMapping.accountCPCMapping;
QInvestigator investigator = QInvestigator.investigator;
JPAQuery<Tuple> query = new JPAQuery<Tuple>(em);
query.select(Projections.bean(Account.class, account.sClientAcctId, account.sAcctDesc, account.sLocation,
Projections.bean(Department.class, department.sDeptName, department.sClientDeptId).as("department"),
Projections.bean(Investigator.class, investigator.sInvestigatorName).as("investigator"),
Projections.bean(AccountCPCMapping.class, accountCPCMapping.sCCPCode).as("accountCPC"))).from(account)
.innerJoin(account.department, department).innerJoin(account.accountCPC, accountCPCMapping)
.innerJoin(account.investigator, …
Run Code Online (Sandbox Code Playgroud) 我的项目中有以下 REST 控制器方法
@RequestMapping(method = GET, value = "applications", produces = {MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody
ResponseEntity<?> getApplications(@QuerydslPredicate(root = Application.class) Predicate predicate,
PersistentEntityResourceAssembler resourceAssembler, Pageable page) {
Page<ApplicationProjection> applications = appRepo.findAll(predicate, page).
map(item -> projectionFactory.createProjection(ApplicationProjection.class, item));
return new ResponseEntity<>(pagedResourcesAssembler.toResource(applications), HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)
现在我想根据条件删除页面的一些元素。如何在 Spring Data Rest 中实现?
我正在使用 queryDsl 通过 Gradle 生成 Q 类。它曾经在 Gradle 3.5 上运行良好,但在升级到 Gradle 5.5.1 时,它因重复类错误而失败。
我的generateQueryDsl 任务可以很好地生成“gensrc/”下的类,但在compileJava 上,这些类会在“build/ generated/”下再次生成,最终会出现重复的类错误。
dependencies {
api("org.springframework.boot:spring-boot-starter-data-jpa") {
exclude group: "org.hibernate", module: "hibernate-entitymanager"
exclude group: "org.hibernate", module: "hibernate-core"
exclude group: "org.apache.tomcat", module: "tomcat-jdbc"
}
api("com.zaxxer:HikariCP:${hikaricpVersion}")
api("com.h2database:h2:1.4.193")
api("mysql:mysql-connector-java")
api("com.microsoft.sqlserver:sqljdbc42:6.0.8112")
api("org.springframework.data:spring-data-jpa")
api("org.springframework:spring-jdbc")
api("org.springframework:spring-orm")
api("org.eclipse.persistence:javax.persistence:${eclipseLinkPersistenceVersion}")
api("org.eclipse.persistence:eclipselink:${eclipseLinkVersion}")
api("org.eclipse.persistence:org.eclipse.persistence.jpa:${eclipseLinkVersion}")
api("com.mysema.querydsl:querydsl-sql:${queryDslVersion}")
api("com.mysema.querydsl:querydsl-jpa:${queryDslVersion}")
api("com.mysema.querydsl:querydsl-apt:${queryDslVersion}")
annotationProcessor('com.mysema.querydsl:querydsl-apt:3.7.4:jpa')
annotationProcessor("org.springframework.boot:spring-boot-starter-data-jpa")
}
task generateQueryDSL(type: JavaCompile, group: 'build) {
source = sourceSets.main.java
classpath = configurations.compileClasspath
options.annotationProcessorPath = configurations.annotationProcessor
destinationDir = file('gensrc/main/java')
}
compileJava {
dependsOn generateQueryDSL
}
error: duplicate class: …
Run Code Online (Sandbox Code Playgroud) 当我尝试将 Spring Boot 应用程序从版本 1.4.2.RELEASE 迁移到版本 2.2.6.RELEASE 时,我发现找不到 org.springframework.data.querydsl.QueryDslPredicateExecutor
无法解析符号 QueryDslPredicateExecutor
我应该添加 spring-data-commons 版本 1.12.8.RELEASE
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.12.8.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
然后我发现了另一个问题
GitHub 存储库:https ://github.com/dali05/SpringBootMultipleMavenModules
thnaks
我尝试将 MapStruct 与 QueryDsl、Spring Boot 3 和 Java 17 一起使用,但maven-compiler-plugin
我用于 MapStruct 的似乎阻止了 QueryDsl 资源的生成。
<dependencies>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>5.0.0</version>
<classifier>jakarta</classifier>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>5.0.0</version>
<classifier>jakarta</classifier>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
如果我删除maven-compiler-plugin
QueryDsl 资源,则会生成但 MapStruct 不会生成。
我还尝试添加 QueryDsl 注释处理器,但没有任何运气。
<path>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>5.0.0</version>
</path>
Run Code Online (Sandbox Code Playgroud)
有什么建议么?
我正在尝试使用 Spring Boot 3、Jakarta Persistence、QueryDsl、Gradle 和 Intellij 生成 q 类。但运行后我无法生成 q-classes
./gradlew clean build
。构建后,我收到此错误:Unable to load class 'jakarta.persistence.Entity'. This is an unexpected error. Please file a bug containing the idea.log file.
我搜索了互联网,但没有找到解决问题的答案,因为 Spring Boot 版本 3 是最新的。
有人能帮我吗?
这是我的 build.gradle
plugins {
id 'org.springframework.boot' version '3.0.1'
id 'io.spring.dependency-management' version '1.1.0'
id 'java'
}
group = 'br.com.sammubr'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories { …
Run Code Online (Sandbox Code Playgroud)