JPAMetaModelEntityProcessor:通过orm.xml映射时,无法确定属性<some array =“” property =“”>“的类型

时间:2019-02-11 19:59:43

标签: java hibernate jpa jpamodelgen

我有一个JPA实体类,它完全由orm.xml配置(没有注释)。实体类的属性之一是String[],它通过String实现在数据库端转换为普通的javax.persistence.AttributeConverter<String[], String>表示形式。我想使用org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor或类似方法为实体生成元模型信息。

但是,JPAMetaModelEntityProcessor似乎忽略了orm.xml中定义的转换,因为它引发以下错误:

--- maven-processor-plugin:2.2.4:process (process) @ ConverterTest ---
diagnostic: warning: Unable to determine type for property tags of class test.convertertest.Post using access type FIELD

如果我使用批注而不是orm.xml进行配置,则工作正常,因此我假设处理器无法从类泛型确定转换类型。但是,我找不到其他明确定义它们的方法。

请注意,如果省略了array属性,则处理器也可以正常运行(具有预期的输出)。

我曾尝试在实体和转换器中将String[]替换为List<String>,在这种情况下,处理器可以顺利完成,但是会生成无法编译的代码:

// Post_.java
public static volatile SingularAttribute<Post, List<E>> tags; // cannot find symbol E

这是未记录的错误/限制,还是我在某处缺少规范?

下面是一个示例实体类,它说明了这个问题(人为设计,主要是从here复制):

public class Post implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;

    // other stuff

    private String[] tags;

    // boilerplate nonsense
}

这是orm.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm http://xmlns.jcp.org/xml/ns/persistence/orm_2_2.xsd" version="2.2">
    <persistence-unit-metadata>
        <xml-mapping-metadata-complete/>
    </persistence-unit-metadata>
    <access>FIELD</access>
    <entity class="test.convertertest.Post" name="Post">
        <table name="POST"/>
        <attributes>
            <id name="id">
                <column name="ID"/>
                <generated-value strategy="AUTO"/>
            </id>
            <!-- other stuff -->
            <basic name="tags">
                <column name="TAGS"/>
                <convert converter="test.convertertest.ListToStringConverter"/>
            </basic>
        </attributes>
    </entity>
</entity-mappings>

转换器:

@Converter
public class ListToStringConverter implements AttributeConverter<String[], String> {

    @Override
    public String convertToDatabaseColumn(String[] attribute) {
        return (attribute == null || attribute.length == 0) ? "" : String.join(",", attribute);
    }

    @Override
    public String[] convertToEntityAttribute(String dbData) {
        return (dbData == null || dbData.isEmpty()) ? new String[0] : dbData.split(",");
    }
}

相关pom.xml条目:

<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <version>2.2.4</version>
    <executions>
        <execution>
            <id>process</id>
            <goals>
                <goal>process</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
                <processors>                                
                    <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                </processors>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>5.4.1.Final</version>
        </dependency>
    </dependencies>
</plugin>

谢谢!

0 个答案:

没有答案