现在,我的元模型类中的每个字段都有两个字段,例如:
public static final String _registrationDate="registrationDate";
public static volatile SingularAttribute<User, Date> registrationDate;
为了让它工作,我重用了来自 JPAMetaModelEntityProcessor 的代码(不幸的是,简单地扩展这个类有问题)。我添加了这个方法:
private void addFieldsNamesAsStrings(MetaEntity entity) {
if (entity instanceof AnnotationMetaEntity) {
AnnotationMetaEntity aentity = (AnnotationMetaEntity) entity;
List<MetaAttribute> newMembers = new ArrayList<MetaAttribute>();
for (final MetaAttribute ma : entity.getMembers()) {
MetaAttribute nma = new AnnotationMetaAttribute(aentity, null,
null) {
public String getDeclarationString() {
return new StringBuilder()
.append("public static final String ")
.append(getPropertyName()).append("=\""+ma.getPropertyName()+"\";")
.toString();
}
@Override
public String getPropertyName() {
return "_"+ma.getPropertyName();
}
@Override
public String getMetaType() {
return null;
}
};
newMembers.add(nma);
aentity.mergeInMembers(newMembers);
}
}
}
我在每次发生之前调用它
ClassWriter.writeFile(entity, context);
对应的maven配置:
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<processors>
<processor>
com.company.MyProcessor
</processor>
</processors>
<outputDirectory>target/modelgen/src/main/java</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>