如何在JPA 2.0元模型中自动生成列名作为静态最终字符串?

vin*_*nga 6 java jpa maven

在一些JPA注释中,我想直接在代码中使用字段名称来代替容易出错的字符串:

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

但上面不会编译,因为获取名称我必须使用非常量表达式的函数(User_生成JPA @StaticMetamodel).

是否有可能以任何方式使用元模型或者我坚持使用直接字符串常量?有没有办法为元模型自动生成这样的字符串常量?(我正在使用maven-processor-plugin进行生成)

vin*_*nga 5

现在我为元模型类中的每个字段获取两个字段,例如:

public static final String _registrationDate="registrationDate";
public static volatile SingularAttribute<User, Date> registrationDate;   
Run Code Online (Sandbox Code Playgroud)

为了实现这一点,我重用了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);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

我在每次出现之前都会调用它

ClassWriter.writeFile(entity, context);
Run Code Online (Sandbox Code Playgroud)

相应的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>
Run Code Online (Sandbox Code Playgroud)


Puc*_*uce 0

我还没有尝试过,但从我在完全其他上下文(不是 JPA)中读到的内容来看,你可以尝试:

  • 指定自定义注释 (RetentionPolicy.SOURCE) 并注释相关实体类(或者您可以仅依赖 @Entity 注释)
  • 编写一个注释处理器,它编写一个带有静态字段的类

例如

public class UserConstants{
    public static final String REGISTRATION_DATE = User_.registrationDate.getName(); 
}
Run Code Online (Sandbox Code Playgroud)

这只是一个想法。我不知道它是否适合这种情况。