【问题标题】:use fully qualified names in hyperjaxb3 generated java classes在 hyperjaxb3 生成的 java 类中使用完全限定名称
【发布时间】:2015-12-03 11:56:52
【问题描述】:

我已经从 hyperjax3 生成了 .java 类,这些类已经用 @Entity 和 @Table 等注释进行了注释。”

在@Entity 中,类名会自动添加如下: @Entity(name = "MyClassName") 但我希望这个名称字段有一个完全限定的类名
@Entity(name = "myPackage.here.MyClassName") 我正在使用 hyperjaxb3-ejb-samples-po-initial-0.5.6 例子 并通过运行 mvn clean install 生成带注释的 java 类,其中我的 XSD 架构存在于 maven 项目的 src\main\resources 文件夹中。

*我已经搜索并找到了一种声明使用 auto-import=false 的方法,但我无法合并它,因为我只是在运行那个 maven 项目。

【问题讨论】:

  • 类似问题请参考here

标签: hibernate jpa jaxb hyperjaxb


【解决方案1】:

免责声明:我是Hyperjaxb3的作者。

实体名称不可自定义,但您可以实施自己的命名策略来生成完全限定的实体名称。

为此,您必须实现org.jvnet.hyperjaxb3.ejb.strategy.naming.Naming 接口。最简单的方法是继承 org.jvnet.hyperjaxb3.ejb.strategy.naming.impl.DefaultNaming 并覆盖 getEntityName 方法:

public String getEntityName(Mapping context, Outline outline, NType type) {
    final JType theType = type.toType(outline, Aspect.EXPOSED);
    assert theType instanceof JClass;
    final JClass theClass = (JClass) theType;
    return CodeModelUtils.getPackagedClassName(theClass);
}

您还必须包含 org\jvnet\hyperjaxb3\ejb\plugin\custom\applicationContext.xml 资源来配置您的自定义命名策略:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean name="naming" class="com.acme.foo.CustomNaming">
        <property name="reservedNames" ref="reservedNames"/>
    </bean>

</beans>

最后,全部编译,打包为 JAR 并添加到 HJ3 类路径,例如通过 Maven POM 中的插件依赖项:

        <plugin>
            <groupId>org.jvnet.hyperjaxb3</groupId>
            <artifactId>maven-hyperjaxb3-plugin</artifactId>
            <configuration>...</configuration>
            <dependencies>
                <dependency>
                    <groupId>com.acme.foo</groupId>
                    <artifactId>hyperjaxb3-custom-naming-extension</artifactId>
                    <version>...</version>
                </dependency>
            </dependencies>
        </plugin>

这是一个实现/配置自定义命名策略的测试项目:

另见:

【讨论】:

    猜你喜欢
    • 2013-06-21
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 2020-11-12
    相关资源
    最近更新 更多