【问题标题】:How do I automatically generate column names as static final strings in JPA 2.0 metamodel?如何在 JPA 2.0 元模型中自动生成列名作为静态最终字符串?
【发布时间】:2012-06-06 02:50:09
【问题描述】:

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

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

但上面的代码无法编译,因为要获取名称,我必须使用不是常量表达式的函数(User_ 是生成的 JPA @StaticMetamodel)。

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

【问题讨论】:

    标签: java maven jpa


    【解决方案1】:

    现在我的元模型类中的每个字段都有两个字段,例如:

    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>
    

    【讨论】:

      【解决方案2】:

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

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

      例如

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

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

      【讨论】:

      • 我认为这是目前最简单的解决方案。
      猜你喜欢
      • 2012-12-13
      • 1970-01-01
      • 2012-04-05
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多