Skip to content

HyperJAXB3 Adding required properties

Laurent Schoelens edited this page May 17, 2024 · 1 revision

Adding required properties

Adding identifier property

Support for composite primary keys

You can mark several properties of your class as identifier properties. In this case these properties will build up a composite primary key. Hyperjaxb3 detects declarations of composite primary keys and automatically generates the required identifier class.

Consider the following schema snippet:

<xsd:complexType name="Parent">
    <xsd:sequence>
        <xsd:element name="children" type="Child" minOccurs="0" maxOccurs="unbounded" />
    </xsd:sequence>
    <xsd:attribute name="pk1" type="xsd:string">
        <xsd:annotation><xsd:appinfo><hj:id /></xsd:appinfo></xsd:annotation>
    </xsd:attribute>
    <xsd:attribute name="pk2" type="xsd:string">
        <xsd:annotation><xsd:appinfo><hj:id /></xsd:appinfo</xsd:annotation>
    </xsd:attribute>
    <xsd:attribute name="parentData" type="xsd:string"/>
</xsd:complexType>

This type declares two id properties (attributes pk1 and pk2). To map this composite primary key, HJ3 will generate a ParentId class:

Parent.java

...
@Entity(name = "org.jvnet.hyperjaxb3.ejb.tests.ids.tests.Parent")
@Table(name = "PARENT")
@IdClass(Parent.ParentId.class)
@Inheritance(strategy = InheritanceType.JOINED)
public class Parent implements Equals, HashCode
{
    ...
 
    @Id
    @Column(name = "PK1")
    public String getPk1() { ... }
 
    ...
 
    @Id
    @Column(name = "PK2")
    public String getPk2() { ... }
 
    ...
 
    public static class ParentId implements Serializable, Equals, HashCode
    {
 
        protected String pk1;
 
        protected String pk2;
 
        public String getPk1() { ... }
 
        public void setPk1(String value) { ... }
 
        public String getPk2() { ... }
 
        public void setPk2(String value) { ... }
 
        public boolean equals(Object object) { ... }
 
        public int hashCode() { ... }
 
    }
}

Many thanks to Constantine Kulak who helped with this feature.

Support for cascading ids

Yet to be developed.

Clone this wiki locally