简体   繁体   English

xsd:Java类中的唯一

[英]xsd:unique in a Java class

The xsd:unique constraint in my schema defines primary key and foreign key for few elements. 我的架构中的xsd:unique约束为几个元素定义了主键和外键。

Though the schema does not throw any error. 虽然架构不会引发任何错误。

While generating java class xsd:unique is not added. 生成Java类时,未添加xsd:unique

<?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>   

JAXB doesn't enforce validation rules. JAXB不执行验证规则。

To enforce validation rules, you need to specify the XSD schema when parsing the XML. 为了执行验证规则,在解析 XML时需要指定XSD模式。

Assuming you're asking the Unmarshaller to parse the XML for you, eg you're not unmarshalling a DOM node, you do it like this: 假设您要让Unmarshaller为您解析XML,例如您没有在对DOM节点进行解组,则可以这样进行:

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 : 这在Unmarshaller的javadoc中进行了描述:

Validation and Well-Formedness 验证和格式正确

A client application can enable or disable JAXP 1.3 validation mechanism via the setSchema(javax.xml.validation.Schema) API. 客户端应用程序可以通过setSchema(javax.xml.validation.Schema) API启用或禁用JAXP 1.3验证机制。 Sophisticated clients can specify their own validating SAX 2.0 compliant parser and bypass the JAXP 1.3 validation mechanism using the unmarshal(Source) API. 复杂的客户端可以指定他们自己的符合SAX 2.0的验证解析器,并使用unmarshal(Source) API绕过JAXP 1.3验证机制。

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. 由于在JAXB 2.0中定义了将无效XML内容解组,因此与JAXB 1.0中相比,解组器默认验证事件处理程序更为宽松。 When schema-derived code generated by JAXB 1.0 binding compiler is registered with JAXBContext , the default unmarshal validation handler is DefaultValidationEventHandler and it terminates the marshal operation after encountering either a fatal error or an error. 当由JAXB 1.0绑定编译器生成的派生于架构的代码向JAXBContext注册时,默认的解组验证处理程序为DefaultValidationEventHandler并且在遇到致命错误或错误后将终止编组操作。 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. 对于JAXB 2.0客户端应用程序,没有显式定义的默认验证处理程序,并且默认事件处理仅在遇到致命错误后才终止解组操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM