【问题标题】:Hibernate JPA 2 Metamodel Generator Turkish Char problemHibernate JPA 2 Metamodel Generator土耳其字符问题
【发布时间】:2019-12-02 14:05:05
【问题描述】:
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jpamodelgen</artifactId>
        <version>6.0.0.Alpha2</version>
    </dependency>

当我向项目添加 hibernate-jpamodelgen 依赖项时。一切正常,直到编译过程。我可以在目标文件夹下看到生成的元模型类。但是由于我的系统默认值(与我的操作系统相关),元模型类上的字段名称常量转换错误。

public static final String TRANST�ME = "transtime";
public static final String NOTE = "note";
public static final String �SACT�VE = "isactive";

-

[ERROR] /C:/Users/*/IdeaProjects/*/target/generated-sources/annotations/*/model/acc/InvtypeView_.java:[20,37] illegal character: '\ufffd'

这会导致编译错误。当我分析代码生成过程时,我可以看到 org.hibernate.jpamodelgen.util.StringUtil 类的 getUpperUnderscoreCaseFromLowerCamelCase 方法导致了这种情况。

public static String getUpperUnderscoreCaseFromLowerCamelCase(String lowerCamelCaseString){
    return lowerCamelCaseString.replaceAll("(.)(\\p{Upper})", "$1_$2").toUpperCase();
}

toUpperCase 方法应该有参数 Locale.ROOT。

我在Hibernate issue tracker system 上创建了一个问题。

任何快速的解决方案/解决方法都会很棒。

【问题讨论】:

  • 我了解问题源于使用非 ASCII 土耳其语“i”字符的变体之一。根据我的经验,尽管 Java 作为一种语言支持 Unicode,但我经常发现该工具不支持,因此我建议避免使用非拉丁字符作为标识符。否则,您可能会设法解决您的问题,但发现另一个工具有问题
  • 您使用什么方法来生成元模型类? Ant 有多种用法,Maven 有多种方法:docs.jboss.org/hibernate/orm/5.0/topical/html/metamodelgen/… - 通过编译器或处理器插件。根据您使用的方法,您可能会找到一种将语言环境变量传递给编译器和编译处理器的方法,并且您无需更改系统设置。
  • @crizzis 我没有在我的代码中使用任何土耳其语字符。问题是代码生成器将“transtime”字段转换为 TRANSTİME 常量名称,而不是 TRANSTIME。
  • @UtkuÖzdemir 我只是在我的 POM 中添加依赖项。请参阅文档:“在大多数情况下,注释处理器将自动运行,只要将处理器 jar 添加到构建类路径并使用 JDK >6。这是由于 Java 的服务提供者合同和 Hibernate 静态元模型生成器 jar 文件的事实包含 META-INF/services 目录中的 javax.annotation.processing.Processor 文件。”
  • @evracle 在这种情况下,您可能面对的是this issue,其中带点的i 变成了大写的İ。您可以尝试通过使用 -Duser.language=en 标志或 &lt;user.language&gt;en&lt;/user.language&gt; 标记来强制将 i 大写为 I 来解决该问题

标签: java hibernate annotation-processing


【解决方案1】:

我已经通过以下配置解决了同样的问题。

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>${java.version}</source>
        <target>${java.version}</target>
        <fork>true</fork>
        <compilerArgs>
            <compilerArg>-J-Duser.language=en</compilerArg>
            <compilerArg>-J-Duser.country=US</compilerArg>
            <compilerArg>-J-Dfile.encoding=UTF-8</compilerArg>
        </compilerArgs>
        <annotationProcessorPaths>
            <annotationProcessorPath>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-jpamodelgen</artifactId>
                <version>${hibernate.version}</version>
            </annotationProcessorPath>
        </annotationProcessorPaths>
    </configuration>
</plugin>

【讨论】:

【解决方案2】:

我也有同样的问题。我的问题通过以下插件解决了

<plugin>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-plugin.version}</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <compilerArgument>-proc:none</compilerArgument>
                <encoding>UTF-8</encoding>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>

            </configuration>
        </plugin>

        <plugin>
            <groupId>org.bsc.maven</groupId>
            <artifactId>maven-processor-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <compilerArguments>-AaddGeneratedAnnotation=false</compilerArguments> <!-- suppress java.annotation -->
                        <processors>
                            <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                        </processors>
                        <outputDirectory>generated</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多