Reputation: 23
The xsd:unique
constraint in my schema defines primary key and foreign key for few elements.
Though the schema does not throw any error.
While generating java class xsd:unique
is not added.
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="urn:schemas-microsoft-com:xml-msdata" schemaLocation="msdata.xsd" />
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="subroot">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" name="set">
<xsd:complexType>
<xsd:sequence>
<xsd:any processContents="lax" />
<xsd:choice>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="Held">
<xsd:complexType>
<xsd:attribute name="_attr1" use="required" type="xsd:integer" />
<xsd:attribute name="_attr2" use="required" type="xsd:integer" />
</xsd:complexType>
</xsd:element>
<xsd:element maxOccurs="unbounded" name="Asses">
<xsd:complexType>
<xsd:attribute name="_attr2" use="required" type="xsd:integer" />
<xsd:attribute name="_attr3" use="required" type="xsd:NCName" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
<xsd:unique name="PK_Held" msdata:PrimaryKey="true">
<xsd:selector xpath="xsd:Held"></xsd:selector>
<xsd:field xpath="@_attr1"></xsd:field>
</xsd:unique>
<xsd:unique name="PK_Asses" msdata:PrimaryKey="true">
<xsd:selector xpath="xsd:Asses"></xsd:selector>
<xsd:field xpath="@_attr2"></xsd:field>
</xsd:unique>
<xsd:keyref name="FK_Held_Asses" refer="PK_Asses">
<xsd:selector xpath="xsd:Held"></xsd:selector>
<xsd:field xpath="@_attr2"></xsd:field>
</xsd:keyref>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Upvotes: 0
Views: 474
Reputation: 159114
JAXB doesn't enforce validation rules.
To enforce validation rules, you need to specify the XSD schema when parsing the XML.
Assuming you're asking the Unmarshaller
to parse the XML for you, e.g. you're not unmarshalling a DOM node, you do it like this:
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(schemaFile); // file or URL
JAXBContext jaxbContext = JAXBContext.newInstance();
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setSchema(schema);
unmarshaller.setEventHandler(handler); // To specify how validation errors should be handled
Object obj = unmarshaller.unmarshal(source);
This is described in the javadoc of Unmarshaller
:
Validation and Well-Formedness
A client application can enable or disable JAXP 1.3 validation mechanism via the
setSchema(javax.xml.validation.Schema)
API. Sophisticated clients can specify their own validating SAX 2.0 compliant parser and bypass the JAXP 1.3 validation mechanism using theunmarshal(Source)
API.Since unmarshalling invalid XML content is defined in JAXB 2.0, the Unmarshaller default validation event handler was made more lenient than in JAXB 1.0. When schema-derived code generated by JAXB 1.0 binding compiler is registered with
JAXBContext
, the default unmarshal validation handler isDefaultValidationEventHandler
and it terminates the marshal operation after encountering either a fatal error or an error. For a JAXB 2.0 client application, there is no explicitly defined default validation handler and the default event handling only terminates the unmarshal operation after encountering a fatal error.
Upvotes: 1