我有以下代码:
import com.querydsl.jpa.impl.JPAQuery;
...
QCustomer customer = QCustomer.customer;
JPAQuery query = new JPAQuery(entityManager);
Customer result = query.from(customer)
.where(customer.firstName.eq("Bob"))
.uniqueResult(customer);
然而,在Eclipse中,它表示uniqueResult
未定义。
的pom.xml
<build>
<plugins>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>4.0.9</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>4.0.9</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>4.0.9</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-core</artifactId>
<version>4.0.9</version>
</dependency>
答案 0 :(得分:2)
我继续前进,避免使用JPAQuery
并使用JPAQueryFactory
代替。以下是我使用的新代码:
JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);
QCustomer customer = QCustomer.customer;
List<Study> result = queryFactory.selectFrom(customer)
.where(customer.firstName.eq("Bob"))
.fetchOne();