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

标签 java maven jpa

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

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

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

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

最佳答案

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

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>

关于java - 如何在 JPA 2.0 元模型中自动生成列名作为静态最终字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10840510/

相关文章:

java - 调用 Java 对象的方法

ADLS Gen2 中文件的 Java 文件对象

java - 局部变量同步

jsf - 我应该使用哪个 jsf-impl?

Spring-data-jpa 渴望获取加入和使用分页不起作用

Spring引导2.1.2, hibernate 5 : Register Hibernate Event Listeners (Insert/Update/Delete)

java - 我们应该在 jpbc 的 params.properties 文件中编写什么

java - 项目制作jar文件后出错

java - 当属性来自另一个表时,如何避免在 Spring Data JPA 上指定 SQL 查询

spring - 为什么没有 spring-asm-3.2.4.RELEASE jar?