これで、メタモデル クラスのすべてのフィールドに対して 2 つのフィールドを取得できます。次に例を示します。
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>