我似乎遇到了一个问题,在我的生活中我无法理解为什么。我怀疑它是一个名称空间问题。
以下代码始终会导致以下错误:DynamicHelper.createQuery:找不到动态类型:
我尝试了很多名称空间
我完全希望这会是用户错误。我们非常欢迎任何指导。
JAVA CODE:
package model;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.xml.bind.JAXBException;
import org.eclipse.persistence.dynamic.DynamicEntity;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class Test
{
public static void main(String[] args)
{
try {
URL schema = new URL("http://localhost:8080/stockcontrol/schema/stockcontrol.xsd");
InputStream xsd = schema.openStream();
EntityResolver er = new EntityResolver()
{
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException
{
URL schema = new URL("http://localhost:8080/stockcontrol/schema/"+(new File(systemId)).getName());
InputStream xsd = schema.openStream();
InputSource is = new InputSource(xsd);
is.setSystemId(schema.getPath());
return is;
}
};
//create the context from the XSD
DynamicJAXBContext xmlContext = DynamicJAXBContextFactory.createContextFromXSD(xsd,er, null, null);
//create a DYNAMICEntity for the supplier object.
DynamicEntity de = xmlContext.newDynamicEntity("mmckenzie.stockcontrol/supplier");
}
catch (IOException | JAXBException e)
{
e.printStackTrace();
}
}
}
XSD
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema elementFormDefault="qualified" version="1.0" targetNamespace="mmckenzie.stockcontrol" xmlns:tns="mmckenzie.stockcontrol" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import schemaLocation="stockcontrol2.xsd"/>
<xs:element name="batch" type="tns:batchDAO"/>
<xs:element name="batches" type="tns:batchesDAO"/>
<xs:element name="component" type="tns:componentDAO"/>
<xs:element name="components" type="tns:componentsDAO"/>
<xs:element name="prefixes" type="tns:componentPrefixesDAO"/>
<xs:element name="product" type="tns:productDAO"/>
<xs:element name="product_batches" type="tns:productBatchesDAO"/>
<xs:element name="productbatch" type="tns:productBatchDAO"/>
<xs:element name="products" type="tns:productsDAO"/>
<xs:element name="supplier" type="tns:supplierDAO"/>
<xs:element name="suppliers" type="tns:suppliersDAO"/>
<xs:complexType name="batchesDAO">
<xs:sequence>
<xs:element name="batch" type="tns:batchDAO" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="batchDAO">
<xs:sequence>
<xs:element name="idbatch" type="xs:int"/>
<xs:element name="dateReceived" type="xs:long"/>
<xs:element name="dateConsumed" type="xs:long"/>
<xs:element name="invoice" type="xs:string" minOccurs="0"/>
<xs:element name="qty" type="xs:int"/>
<xs:element name="qtyInStock" type="xs:int"/>
<xs:element name="unitCost" type="xs:decimal" minOccurs="0"/>
<xs:element name="componentId" type="xs:int"/>
<xs:element name="supplierId" type="xs:int"/>
<xs:element name="unitType" type="unitType" minOccurs="0"/>
<xs:element name="isActive" type="xs:boolean"/>
<xs:element ref="tns:products" minOccurs="0"/>
<xs:element name="batchCost" type="xs:decimal" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="productsDAO">
<xs:sequence>
<xs:element name="product" type="tns:productDAO" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="productDAO">
<xs:sequence>
<xs:element name="idProduct" type="xs:int"/>
<xs:element name="cost" type="xs:decimal" minOccurs="0"/>
<xs:element name="description" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:int"/>
<xs:element name="sellPrice" type="xs:decimal" minOccurs="0"/>
<xs:element name="productBatches" type="tns:productBatchesDAO" minOccurs="0"/>
<xs:element name="date_sold" type="timestamp" minOccurs="0"/>
<xs:element name="date_produced" type="timestamp" minOccurs="0"/>
<xs:element ref="tns:components" minOccurs="0"/>
<xs:element name="hours_worked" type="xs:double"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="productBatchesDAO">
<xs:sequence>
<xs:element name="productbatches" type="tns:productBatchDAO" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="productBatchDAO">
<xs:sequence>
<xs:element ref="tns:component" minOccurs="0"/>
<xs:element ref="tns:batch" minOccurs="0"/>
<xs:element name="quantyUsed" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="componentDAO">
<xs:sequence>
<xs:element name="idcomponent" type="xs:int"/>
<xs:element name="componentName" type="xs:string" minOccurs="0"/>
<xs:element ref="tns:batches" minOccurs="0"/>
<xs:element name="componentPrefix" type="componentPrefix" minOccurs="0"/>
<xs:element name="qtyInStock" type="xs:int"/>
<xs:element ref="tns:products" minOccurs="0"/>
<xs:element name="quantity" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="componentsDAO">
<xs:sequence>
<xs:element name="component" type="tns:componentDAO" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="suppliersDAO">
<xs:sequence>
<xs:element name="supplier" type="tns:supplierDAO" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="supplierDAO">
<xs:sequence>
<xs:element name="idsupplier" type="xs:int"/>
<xs:element name="modified" type="timestamp" minOccurs="0"/>
<xs:element name="name" type="xs:string" minOccurs="0"/>
<xs:element ref="tns:batches" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="componentPrefixesDAO">
<xs:sequence>
<xs:element name="prefix" type="componentPrefix" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
欢迎所有评论建议。
答案 0 :(得分:0)
它创建命名空间的方式并不是我所期望的。这个例子的正确答案是
DynamicEntity de = xmlContext.newDynamicEntity("stockcontrol.mmckenzie.SupplierDAO");
本文有助于找到解决方案。 附录D:将XML名称绑定到Java标识符&#34; Java绑定的Java体系结构(JAXB)2.2规范(http://jcp.org/en/jsr/detail?id=222)
然而,使用eclipse调试器,我能够找到Helper名称空间。要使用此方法,请在eclipse(Mars in mycase)中执行以下步骤。
键值可以让您了解它所期望的命名空间。