我正在尝试将QueryDSL与SpringData一起使用,并使用Mongo存储库。
以下是我的三个文档类层次结构的简化:
顶级实体类
public abstract class AbstractEntity<T extends Serializable> {
public abstract T getId();
...
}文档(Mongo)实体的基类
import org.springframework.data.annotation.Id;
import com.querydsl.core.annotations.QueryEntity;
@QueryEntity
public abstract class DocumentEntity extends AbstractEntity<String> {
@Id
private String id;
public DocumentEntity() {
}
public String getId() {
return id;
}
...
}实际实体/文件执行情况
import org.springframework.data.mongodb.core.index.TextIndexed;
import org.springframework.data.mongodb.core.mapping.Document;
import com.blah.data.entity.DocumentEntity;
import com.querydsl.core.annotations.QueryEntity;
@QueryEntity
@Document(collection="Manufacturers")
public class Manufacturer extends DocumentEntity {
@TextIndexed
private String name;
public Manufacturer() {
}
...
}我相信,这里的主要版本号是
下面是我的插件配置:
<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> org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
在我的maven构建中成功地生成了"Q“类。但是,我的QDocumentEntity查询类扩展了BeanPath,而我的QManufacturer查询类扩展了EntityPathBase。这似乎是问题的根源。
当我启动我的应用程序时,我会得到一个异常,其根本原因如下:
Caused by: java.lang.ClassCastException: com.blah.data.entity.QDocumentEntity cannot be cast to com.querydsl.core.types.EntityPath
at org.springframework.data.querydsl.SimpleEntityPathResolver.createPath(SimpleEntityPathResolver.java:59)
at org.springframework.data.mongodb.repository.support.QueryDslMongoRepository.<init>(QueryDslMongoRepository.java:85)
at org.springframework.data.mongodb.repository.support.QueryDslMongoRepository.<init>(QueryDslMongoRepository.java:67)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)基于此错误,超类DocumentEntity似乎也应该在扩展EntityPathBase时生成。
我不确定它是否相关,但是AbstractEntity和DocumentEntity类位于一个单独的maven模块中。
我是不是遗漏了标记超类作为插件实体的东西?如何迫使生成器将我的超类视为实体?
发布于 2018-02-03 17:15:45
到目前为止,我已经发现了两件事。在这一点上,这两件事足以作为一个解决办法。
我在Eclipse中运行了maven构建,这样我就可以逐步完成并调试注释处理器正在做的事情。这使我第一次意识到。
在com.querydsl.apt.ExtendedTypeFactory.createClassType().中,我的父类DocumentEntity没有被识别为实体。最初确定的类型是TypeCategory.SIMPLE,它从未切换到TypeCategory.ENTITY,因为父类中没有定义的entityAnnotations。为了将其识别为一个实体,必须有一个已定义的注释:实体、超类型或可嵌入的。Springframework的MongoAnnotationProcessor将这些定义为Document.class、QuerySupertype.class和QueryEmbeddable.class。
因此,我将@QuerySupertype添加到我的DocumentEntity类中,得到了一个不同的错误。
/target/generated-sources/java/com/blah/entity/QManufacturer.java:22: error: cannot find symbol
public final com.blah.data.entity.QDocumentEntity _super = new com.blah.data.entity.QDocumentEntity(this);
^
symbol: class QDocumentEntity
location: package com.blah.data.entity
1 error正如我在OP中提到的,父类(Es)位于另一个模块中。如果我将DocumentEntity (带有适当的注释)移动到相同的模块中,它似乎会像预期的那样工作。QDocumentEntity类正确地扩展了EntityPathBase。
我不会将答案标记为已被接受的,因为在我的脑海中,即使我能够做出改变并继续前进,它也没有得到完全的回答。
我不知道这是否与每个模块的APT处理的时间有关。
我真的希望有一个基本的“文档”实体类,它位于一个可共享的模块中,然后从该模块中扩展特定的子模块,并且仍然生成适当的Q类。我的假设是我还漏掉了什么。
当时间允许的时候,我可能会尝试更多地通过适当的处理,看看我是否能找到它的底部。
https://stackoverflow.com/questions/48543695
复制相似问题