简体   繁体   中英

use fully qualified names in hyperjaxb3 generated java classes

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

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 example and 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.

*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.

Disclaimer: I am the author of Hyperjaxb3 .

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

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);
}

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>

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:

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM