具有外键的XSD架构/对JAXB模型的引用

时间:2015-08-31 18:19:07

标签: java xml xsd jaxb

我尝试使用CJX编译器从XSD架构生成Java JAXB模型。

现在我想创建一个从 Connection 到特定主机的关系。 因此,当您选择连接时,您还会收到特定的主机。

如果我们使用以下示例:

  • 当您选择 ID = 1 连接时,您将收到 主机 id:2
  • 如果您选择 ID = 2 连接,您将收到 主机 id:233223231 甚至不存在......

问题:

当你查询时我想要那个; SELECT主机FROM连接WHERE id = 1 您将Host作为对象(就像在SQL中一样?),现在您只需获取一个ID,我必须再次循环才能找到主机。

我错了什么,或者我该怎么办?我猜我的主要密钥和外键不起作用。所以XSD文件中有问题。

欢迎所有反馈,建议等。提前谢谢!

project.xsd

<xs:schema targetNamespace="http://www.nberlijn.nl/project/persistence/models"
           xmlns="http://www.nberlijn.nl/project/persistence/models"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">

    <!-- MAIN -->
    <xs:element name="project">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="hosts"/>
                <xs:element ref="connections"/>
            </xs:sequence>
        </xs:complexType>

        <!-- PRIMARY KEY'S -->
        <xs:key name="PKHost">
            <xs:selector xpath="hosts/host"/>
            <xs:field xpath="id"/>
        </xs:key>
        <xs:key name="PKConnection">
            <xs:selector xpath="connections/connection"/>
            <xs:field xpath="id"/>
        </xs:key>

        <!-- FOREIGN KEY'S -->
        <xs:keyref name="FKConnectionToHost" refer="PKHost">
            <xs:selector xpath="connections/connection"/>
            <xs:field xpath="host"/>
        </xs:keyref>
    </xs:element>

    <!-- REFERENCES -->
    <xs:element name="hosts">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="host" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="connections">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="connection" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <!-- REFERENCES -->
    <xs:element name="host">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="address" type="xs:string"/>
                <xs:element name="port" type="xs:long"/>
                <xs:element name="timeout" type="xs:long"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:int"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="connection">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="host" type="xs:int"/>
                <xs:element name="username" type="xs:string"/>
                <xs:element name="password" type="xs:string"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:int"/>
        </xs:complexType>
    </xs:element>

</xs:schema>

xjc.bat

"C:\Program Files\Java\jdk1.8.0_51\bin\xjc.exe" -d src project.xsd
pause

Main.java

import nl.nberlijn.project.persistence.models.*;

import javax.xml.bind.*;
import java.io.File;
import java.util.List;

public class Main {

    public static void main(String[] args) throws Exception {
        marshaller();
        unmarshaller();
    }

    private static void marshaller() throws JAXBException {
        ObjectFactory objectFactory = new ObjectFactory();

        Project project = objectFactory.createProject();

        Hosts hosts = objectFactory.createHosts();
        Connections connections = objectFactory.createConnections();

        List<Host> hostList = hosts.getHost();
        List<Connection> connectionList = connections.getConnection();

        Host host = objectFactory.createHost();
        Host host2 = objectFactory.createHost();
        Connection connection = objectFactory.createConnection();
        Connection connection2 = objectFactory.createConnection();

        host.setId(1);
        host.setName("Host");
        host.setAddress("host.com");
        host.setPort(22);
        host.setTimeout(10000);

        host2.setId(2);
        host2.setName("Host 2");
        host2.setAddress("host2.com");
        host2.setPort(22);
        host2.setTimeout(10000);

        connection.setId(1);
        connection.setHost(2);
        connection.setUsername("user");
        connection.setPassword("pass");

        connection2.setId(233223231);
        connection2.setHost(1);
        connection2.setUsername("user2");
        connection2.setPassword("pass2");

        hostList.add(host);
        hostList.add(host2);
        connectionList.add(connection);
        connectionList.add(connection2);

        project.setHosts(hosts);
        project.setConnections(connections);

        File file = new File(System.getProperty("user.dir") + "/project.xml");

        JAXBContext jaxbContext = JAXBContext.newInstance(Project.class);

        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(project, System.out);
        marshaller.marshal(project, file);
    }

    private static void unmarshaller() throws JAXBException {
        File file = new File(System.getProperty("user.dir") + "/project.xml");

        JAXBContext jaxbContext = JAXBContext.newInstance(Project.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        Object object = unmarshaller.unmarshal(file);

        Project project = (Project) object;
        Connections connections = project.getConnections();
        List<Connection> connectionList = connections.getConnection();

        int id = 1;

        System.out.println("\nSearch for a connection with id: " + id);

        for (Connection connection : connectionList) {
            if (connection.getId().equals(id)) {
                System.out.println("Connection with id: " + connection.getId() + " found!");

                Hosts hosts = project.getHosts();
                List<Host> hostList = hosts.getHost();

                System.out.println("Search for a host with id: " + connection.getHost());

                for (Host host : hostList) {
                    if (host.getId() == connection.getHost()) {
                        System.out.println("Host found: " + host.getName());
                    } else {
                        System.out.println("Host not found!");
                    }
                }
            }
        }
    }

}

输出继电器:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<project xmlns="http://www.nberlijn.nl/project/persistence/models">
    <hosts>
        <host id="1">
            <name>Host</name>
            <address>host.com</address>
            <port>22</port>
            <timeout>10000</timeout>
        </host>
        <host id="2">
            <name>Host 2</name>
            <address>host2.com</address>
            <port>22</port>
            <timeout>10000</timeout>
        </host>
    </hosts>
    <connections>
        <connection id="1">
            <host>2</host>
            <username>user</username>
            <password>pass</password>
        </connection>
        <connection id="233223231">
            <host>1</host>
            <username>user2</username>
            <password>pass2</password>
        </connection>
    </connections>
</project>

Search for a connection with id: 1
Connection with id: 1 found!
Search for a host with id: 2
Host not found!
Host found: Host 2

0 个答案:

没有答案