问题
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.
回答1:
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:
- https://github.com/highsource/hyperjaxb3/tree/master/ejb/tests/custom-naming
See also:
- Hyperjaxb3 naming strategy configuration
- http://confluence.highsource.org/display/HJ3/Customization+Guide (Server seems to be down at the moment)
来源:https://stackoverflow.com/questions/34065476/use-fully-qualified-names-in-hyperjaxb3-generated-java-classes