0

我想通过使用 hj:generated-value tag 将现有属性标记为生成的标识符来自定义我的 hyperjaxb3 生成的类。在我的 bindings.jxb 文件中,我将 Status 的 id 标记为标识符,但生成的实体未使用 @GeneratedValue 进行注释。因此,Status 表的 id 字段在数据库中被标记为 pkey 但不是自动递增的。

XML 模式定义:

<complexType name="Status">
<sequence>
<element name="id" type="long">
</element>
<element name="title" type="string"></element>
<element name="description" type="cm:LongText"></element>
</sequence>
</complexType>

bindings.xjb 中的 Hyperjaxb3 自定义:

<jaxb:bindings node="xs:complexType[@name='Status']">
            <jaxb:bindings node=".//xs:element[@name='id']">
                <hj:id> <hj:generated-value strategy="AUTO"/> 
                    </hj:id>
            </jaxb:bindings>
        </jaxb:bindings>

结果类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Status", propOrder = {
    "id",
    "title",
    "description"
})
@XmlRootElement(name = "status")
@Entity(name = "Status")
@Table(name = "STATUS")
@Inheritance(strategy = InheritanceType.JOINED)
public class Status
    implements Equals, HashCode
{

    protected long id;
    @XmlElement(required = true)
    protected String title;
    @XmlElement(required = true)
    protected String description;

    /**
     * Gets the value of the id property.
     * 
     */
    @Id
    @Column(name = "ID", scale = 0)
    public long getId() {
        return id;
    }

    /**
     * Sets the value of the id property.
     * 
     */
    public void setId(long value) {
        this.id = value;
    }

    @Transient
    public boolean isSetId() {
        return true;
    }
    ......

在 Postgres 9.2 中使用 hibernate 作为 jpa 提供程序。pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>de.fhg.fokus.cm</groupId>
    <artifactId>fixEMobility</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>Fix eMobility</name>
    <dependencies>
        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.dtd-parser</groupId>
            <artifactId>dtd-parser</artifactId>
            <version>1.0</version>
        </dependency>
    <dependency>
            <groupId>relaxngDatatype</groupId>
            <artifactId>relaxngDatatype</artifactId>
            <version>20020414</version>
        </dependency>
        <dependency>
            <groupId>com.sun.org.apache.xml.internal</groupId>
            <artifactId>resolver</artifactId>
            <version>20050927</version>
        </dependency>
        <dependency>
            <groupId>org.kohsuke.rngom</groupId>
            <artifactId>rngom</artifactId>
            <version>20061207</version>
        </dependency>
        <dependency>
            <groupId>com.sun.istack</groupId>
            <artifactId>istack-commons-tools</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
        <groupId>org.codehaus.jettison</groupId>
        <artifactId>jettison</artifactId>
        <version>1.1</version>
        <!-- <type>bundle</type> -->
    </dependency>

        <dependency>
        <groupId>net.sourceforge.jtds</groupId>
        <artifactId>jtds</artifactId>
        <version>1.2.6</version>
    </dependency>
    <dependency>
        <groupId>xml-apis</groupId>
        <artifactId>xml-apis</artifactId>
        <version>1.0.b2</version>
    </dependency>
    <dependency>
        <groupId>xmlunit</groupId>
        <artifactId>xmlunit</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xsom</groupId>
        <artifactId>xsom</artifactId>
        <version>20081112</version>
    </dependency>
    <dependency>
        <groupId>org.jvnet</groupId>
        <artifactId>mimepull</artifactId>
        <version>1.3</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>
        <version>2.0.7</version>
    </dependency>
<!-- dependencies from the hyperjaxb sample -->
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.2.4-1</version>
    </dependency>
    <dependency>
        <groupId>org.jvnet.hyperjaxb3</groupId>
        <artifactId>hyperjaxb3-ejb-runtime</artifactId>
        <version>0.5.6</version>
    </dependency>

    <!-- Test dependencies -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.1</version>
    </dependency>

    <!-- Roundtrip -->
    <dependency>
        <groupId>org.jvnet.hyperjaxb3</groupId>
        <artifactId>hyperjaxb3-ejb-roundtrip</artifactId>
        <version>0.5.6</version>
    </dependency>

    <!-- Hibernate Dependencies -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.6.5.Final</version>
    </dependency>
    <!-- Database -->
    <!--Database dependency to test without installation-->
    <dependency>
        <groupId>hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>1.8.0.7</version>
        <scope>test</scope>
    </dependency>
    <!--For usage with Postgre 9.x-->
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901.jdbc4</version>
    </dependency>
    <!-- Jax Rs Dependencies -->
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.2</version>
        <!-- <type>bundle</type> -->
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.2</version>
        <!-- <type>bundle</type> -->
    </dependency>
    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-multipart</artifactId>
        <version>1.8</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
<repositories>
    <repository>
        <id>maven2-repository.jboss.com</id>
        <url>http://repository.jboss.com/maven2</url>
    </repository>
</repositories>
<build>
    <defaultGoal>install</defaultGoal>
    <plugins>
        <plugin>
            <groupId>org.jvnet.hyperjaxb3</groupId>
            <artifactId>maven-hyperjaxb3-plugin</artifactId>
            <version>0.5.6</version>
            <executions>
                <execution>
                <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
        <extension>true</extension>                          <roundtripTestClassName>RoundtripTest</roundtripTestClassName>
            </configuration>
        </plugin>
        <plugin>
            <inherited>true</inherited>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>

任何帮助,将不胜感激。

4

1 回答 1

0

改用orm:generated-value

<jaxb:bindings node=".//xs:element[@name='id']">
   <hj:id> 
     <orm:generated-value strategy="AUTO"/> 
   </hj:id>
</jaxb:bindings>
于 2013-08-28T07:15:21.583 回答