本文介绍了在hyperjaxb3生成的java类中使用完全限定名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have generated .java classes from the hyperjax3 which are already annotated with Annotations like @Entity and @Table etc.."

在@Entity中,类名自动添加如下:
@Entity(name =MyClassName)
但我希望此名称字段具有一个完全限定的类名,如
@Entity(name =myPackage.here.MyClassName)
我使用
示例
并通过运行 mvn clean install 生成带注释的java类,其中存在我的XSD模式在maven项目的 src\main\resources 文件夹中。

In @Entity the class name is automatically added as follows:@Entity(name = "MyClassName")But I want this name field to have a fully qualified class name as
@Entity(name = "myPackage.here.MyClassName")I am using the hyperjaxb3-ejb-samples-po-initial-0.5.6 exampleand generating the annotated java classes by running mvn clean install where my XSDs schemas are present in the src\main\resources folder in maven project.

*我搜索并找到了一种方法其中声明使用auto-import = false 但是我无法将其合并,因为我只是运行maven pro ject。

*I have searched and found a way which states that use auto-import=false but I am not able to incorporate this as I am simply running that maven project.

推荐答案

免责声明:我是。

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

Entity name is not customizable, but you can implement your own naming strategy to generate fully qualified entity names.

为此,您必须强制执行 org.jvnet.hyperjaxb3.ejb.strategy.naming。命名界面。最简单的方法是继承 org.jvnet.hyperjaxb3.ejb.strategy.naming.impl.DefaultNaming 并覆盖 getEntityName 方法:

For this, you'd have to impement the org.jvnet.hyperjaxb3.ejb.strategy.naming.Naming interface. The easiest would be to subclass org.jvnet.hyperjaxb3.ejb.strategy.naming.impl.DefaultNaming and to override the getEntityName method:

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 用于配置自定义命名策略的资源:

You'll also have to include the org\jvnet\hyperjaxb3\ejb\plugin\custom\applicationContext.xml resource to configure your custom naming strategy:

<?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中:

Finally, compile it all, pack as JAR and add to the HJ3 classpath, for instance via plugin dependencies in the 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>

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

Here's a test project which implements/configures a custom naming stratgy:



  • https://github.com/highsource/hyperjaxb3/tree/master/ejb/tests/custom-naming

另见:



  • (服务器目前似乎已关闭)

  • Hyperjaxb3 naming strategy configuration
  • http://confluence.highsource.org/display/HJ3/Customization+Guide (Server seems to be down at the moment)

这篇关于在hyperjaxb3生成的java类中使用完全限定名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 23:47