How to automatically generate column names as static trailing rows in a JPA 2.0 metamodel?

In some JPA annotations, I want to use field names directly in the code instead of error prone lines:

@javax.persistence.OrderBy(value = User_.registrationDate.getName())
public List<PlugConfig> getPlugConfigs() { ... }

But the above will not compile, because to get the name I need to use a function that is not a constant expression (User_ generated by JPA @StaticMetamodel).

Is there any way to use the metamodel for this, or am I sticking to direct string constants? Is there a way to automatically generate such string constants for a metamodel? (I use maven-processor-plugin to generate)

+5
source share
2 answers

, , ( JPA), :

  • (RetentionPolicy.SOURCE) ( @Entity).
  • ,

.

public class UserConstants{
    public static final String REGISTRATION_DATE = User_.registrationDate.getName(); 
}

. , .

0

, :

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>
+5

All Articles