-
Notifications
You must be signed in to change notification settings - Fork 99
HyperJAXB3 How to Add a Foreign Key Constraint
In our code, we have a
USER
table and aCONNECTIONS
tableWe need a foreign key in the
CONNECTIONS
table which should refer to theID
column in theUSER
table.<Connection> <User id="Scott Tiger"/> </Connection> ... <User id="Scott Tiger"> <login>scott</login> <password>tiger</password> </User>
Related Issue: HJIII-86
Associations are normally modelled in JPA with @OneToMany
, @ManyToOne
and so on. In the question above the requirement is actually not to model the association, but to simply add a foreign key constraint. To have something like:
alter table CONNECTION_TABLE
add constaint FK1DB6BCCB5C8B695A
foreign key (USER_ID) references USER_TABLE;
This has quite little to do with HJ3 or JPA since this foreign key does not exist in JPA entities, it's purely a database thing. If you still want this foreign key to be generated with JPA means, you can add a secondary table annotation to the entity which maps onto the referenced table.
Here's an example from ejb/tests/issues
:
<xsd:complexType name="issueHJIII86AType">
<xsd:annotation>
<xsd:appinfo>
<hj:entity>
<orm:secondary-table name="ISSUEHJIII86BTYPE">
<orm:primary-key-join-column name="A_" referenced-column-name="ID"/>
</orm:secondary-table>
</hj:entity>
</xsd:appinfo>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="login" type="xsd:string" minOccurs="0" />
<xsd:element name="password" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="id" use="required" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
<hj:id>
<hj:generator generatorClass="assigned" />
</hj:id>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:complexType name="issueHJIII86BType">
<xsd:sequence>
<xsd:element name="A" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="id" use="required" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
<hj:id>
<hj:generator generatorClass="assigned" />
</hj:id>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
Here's a sample XML:
<issueHJIII86A id="issueHJIII86A[0]">
<login>scott</login>
<password>tiger</password>
</issueHJIII86A>
<issueHJIII86B id="issueHJIII86B[0]">
<A>issueHJIII86A[0]</A>
</issueHJIII86B>
In this example ISSUEHJIII86BTYPE
has a foreign key referencing ISSUEHJIII86ATYPE
. To achieve this, I've added a secondary table to the IssueHJIII86AType
, referenced entity. The name of the secondary table matches the name of the table of the referencing entity. Primary key join column establishes a reference between the id column of the referenced entity (IssueHJIII86AType.Id
) and the FK column of the referencing entity (IssueHJIII86BType.A
). This results in a desired foreign key constraint:
alter table ISSUEHJIII86BTYPE
add constraint FK42DC84B0182F5598
foreign key (A_)
references ISSUEHJIII86ATYPE;
In order not to violate this constraint you'll have to make sure that your entities are persisted in the right order.
- Home
- Migration guide
-
JAXB Maven Plugin
- Quick Start
-
User Guide
- Basic Usage
- Specifying What To Compile
- Referencing Resources in Maven Artifacts
- Using Catalogs
- Using Episodes
- Modular Schema Compilation
- Controlling the Output
- Using JAXB Plugins
- Using a Specific JAXB Version
- Configuring Extension, Validation and XML Security
- IDE Integration
- Miscellaneous
- Configuring Proxies
- Maven Documentation
- Configuration Cheat Sheet
- Common Pitfalls and Problems
-
JAXB2 Basics Plugins
- Using JAXB2 Basics Plugins
- JSR-305 Support
-
JAXB2 Basics Plugins List
- SimpleEquals Plugin
- SimpleHashCode Plugin
- Equals Plugin
- HashCode Plugin
- ToString Plugin
- Copyable Plugin
- Mergeable Plugin
- Inheritance Plugin
- AutoInheritance Plugin
- Wildcard Plugin
- Setters Plugin
- Simplify Plugin
- EnumValue Plugin
- JAXBIndex Plugin
- FixJAXB1058 Plugin
- Commons Lang Plugin
- Default Value Plugin
- Fluent API Plugin
- Namespace Prefix Plugin
- Value Constructor Plugin
- Boolean Getter Plugin
- CamelCase Plugin
- XML ElementWrapper Plugin
- Parent Pointer Plugin
- Property Listener Injector Plugin
- Annox
- JAXB Annotate Plugin
-
HyperJAXB3
- Build System Support
- Customization Guide
- Databases
- Development guide
- Extension guide
- FAQ
- IDE Support
- Java Persistence
- JAXB
- JDK Support
- Project Templates
-
Reference
- Adding vendor-specific annotations
- Features
- Integrating Hyperjaxb3 in builds
- Introduction
- Making schema-derived classes ready for JPA
- Adding required properties
- Applying workarounds for JAXB vs. JPA conflicts
- Enforcing top-level classes
- Generating equals and hashCode methods
- Generating ORM metadata
- Generating persistence unit descriptor
- JPA 2 Support
- Making classes serializable
- Testing generated mappings
- Reference - single page
- Related Projects
- Sample projects
- Solutions
- Target Scenarios
- Test Projects
- Tutorials
- Best Practices
- FAQ
- Sample Projects
- Support
- License
- Distribution