users@glassfish.java.net

RE: TopLink Essentials: Mixing

From: Martin Gainty <mgainty_at_hotmail.com>
Date: Wed, 23 Mar 2011 16:03:07 -0400

Here is the code:

// Copyright (c) 1998, 2005, Oracle. All rights reserved.
package examples.ox;

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import examples.ox.model.Customer;
import examples.ox.model.PhoneNumber;

public class OXDemo {
    public static void main(String[] args) throws Exception {
        // Create the JAXBContext
        JAXBContext jaxbContext = JAXBContext.newInstance("examples.ox.model");

        // Unmarshal the XML document to a Java object
        File file = new File("Customer-data.xml");
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        Customer customer = (Customer) unmarshaller.unmarshal(file);

        // Modify the Java object
        PhoneNumber homePhoneNumber = new PhoneNumber();
        homePhoneNumber.setType("home");
        homePhoneNumber.setNumber("(613) 555-3333");
        customer.getPhoneNumbers().add(homePhoneNumber);
        
        // Marshal the Java object back to an XML document
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(customer, System.out);
    }
}

//Customer-data.xml (read only input)
<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSPY v5 rel. 2 U (http://www.xmlspy.com)-->
<c:customer xmlns:c="urn:customer-example" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:customer-example
C:\Prototype\CustomerExample\run\Customer.xsd">
    <personal-info>
        <first-name>Angela</first-name>
        <last-name>Merkel</last-name>
        <c:gender>F</c:gender>
        <date-of-birth>1975-02-21</date-of-birth>
    </personal-info>
    <c:contact-info>
        <billing-address>
            <street>Brandenberg Gate</street>
            <street>Suite 1</street>
            <city>Berlin</city>
            <state>DE</state>
            <zip-code>A1B 2C3</zip-code>
        </billing-address>
        <shipping-address>
            <street>456 Champ Elyssee</street>
            <street>Unit 1</street>
            <city>Paris</city>
            <state>FR</state>
            <zip-code>12345</zip-code>
        </shipping-address>
        <c:phone-number type="work">030-(613) 555-1111</c:phone-number>
        <c:phone-number type="cell">030-(613) 555-2222</c:phone-number>
    </c:contact-info>
</c:customer>


//find all refs to DefaultXMLDocumentHandler
$TOPLINK\examples\foundation\non-relational\ox>grep -S -l DefaultXMLDocumentHandler *.*
.\stage\oracle\xml\comp\CXMLHandlerBase.class
.\stage\oracle\xml\jaxp\JXSAXData.class
.\stage\oracle\xml\parser\v2\DefaultXMLDocumentHandler.class
.\stage\oracle\xml\parser\v2\DocumentBuilder.class
.\stage\oracle\xml\parser\v2\XMLContentHandler.class
.\stage\oracle\xml\parser\v2\XMLTokenizer.class

$TOPLINK\examples\foundation\non-relational\ox>build.cmd
$TOPLINK\examples\foundation\non-relational\ox>runExample

$TOPLINK\examples\foundation\non-relational\ox>title TLOX Application Run script
$TOPLINK\examples\foundation\non-relational\ox>call C:\ANT\APACHE~1.2\bin\ant
runExample -DexampleClass=examples.ox.OXDemo -verbose
Apache Ant(TM) version 1.8.2 compiled on December 20 2010
Trying the default build file: build.xml
Buildfile: $TOPLINK\examples\foundation\non-relational\ox\build.xml
Detected Java version: 1.6 in: $JDK_HOME\jre
Detected OS: Windows XP
parsing buildfile C:\toplink\examples\foundation\non-relational\ox\build.xml
with URI = file:/C:/toplink/examples/foundation/non-relational/ox/build.xml
Project base dir set to: C:\toplink\examples\foundation\non-relational\ox
Build sequence for target(s) `runExample' is [init, verify.build.done, runExample]
Complete build sequence is [init, verify.build.done, runExample, create.stage, default, compile, delete.stage, ]

init:
parsing buildfile jar:file:/$ANT_HOME/lib/ant.jar!/org/apache/tools/ant/antlib.xml
with URI = jar:file:/$ANT_HOME/lib/ant.jar!/org/apache/tools/ant/antlib.xml from a zip file
 [property] Loading $TOPLINK\examples\foundation\examples.properties
 [property] Loading $TOPLINK\examples\foundation\non-relational\ox\env.propert
ies
dropping C:\somedirectory\toplink\jlib\toplink.jar from path as it doesn't exist

dropping C:\somedirectory\lib\xmlparserv2.jar from path as it doesn't exist
dropping C:\somedirectory\lib\xml.jar from path as it doesn't exist
dropping C:\somedirectory\j2ee\home\lib\connector.jar from path as it doesn't ex
ist
dropping C:\somedirectory\toplink\jlib\toplink.jar from path as it doesn't exist

dropping C:\somedirectory\lib\xmlparserv2.jar from path as it doesn't exist
dropping C:\somedirectory\lib\xml.jar from path as it doesn't exist
dropping C:\somedirectory\j2ee\home\lib\connector.jar from path as it doesn't ex
ist

verify.build.done:
Skipped because property 'build.done' set.

runExample:
dropping C:\somedirectory\toplink\jlib\toplink.jar from path as it doesn't exist

dropping C:\somedirectory\lib\xmlparserv2.jar from path as it doesn't exist
dropping C:\somedirectory\lib\xml.jar from path as it doesn't exist
dropping C:\somedirectory\j2ee\home\lib\connector.jar from path as it doesn't ex
ist
dropping C:\somedirectory\toplink\jlib\toplink.jar from path as it doesn't exist

dropping C:\somedirectory\lib\xmlparserv2.jar from path as it doesn't exist
dropping C:\somedirectory\lib\xml.jar from path as it doesn't exist
dropping C:\somedirectory\j2ee\home\lib\connector.jar from path as it doesn't ex
ist
     [java] Executing 'C:\Java\jdk1.6.0_23\jre\bin\java.exe' with arguments:
     [java] '-classpath'
     [java] 'C:\toplink\target\classes;C:\maven-plugin\xmlparserv2\target\classe
s;C:\toplink\examples\foundation\non-relational\ox\config;C:\toplink\examples\fo
undation\non-relational\ox\stage'
     [java] 'examples.ox.OXDemo'
     [java]
     [java] The ' characters around the executable and arguments are
     [java] not part of the command.
dropping C:\somedirectory\toplink\jlib\toplink.jar from path as it doesn't exist

dropping C:\somedirectory\lib\xmlparserv2.jar from path as it doesn't exist
dropping C:\somedirectory\lib\xml.jar from path as it doesn't exist
dropping C:\somedirectory\j2ee\home\lib\connector.jar from path as it doesn't ex
ist
dropping C:\somedirectory\toplink\jlib\toplink.jar from path as it doesn't exist

dropping C:\somedirectory\lib\xmlparserv2.jar from path as it doesn't exist
dropping C:\somedirectory\lib\xml.jar from path as it doesn't exist
dropping C:\somedirectory\j2ee\home\lib\connector.jar from path as it doesn't ex
ist
     [java] <?xml version="1.0" encoding="UTF-8"?>
     [java] <ns1:customer xmlns:ns1="urn:customer-example">
     [java] <personal-info>
     [java] <first-name>Angela</first-name>
     [java] <last-name>Merkel</last-name>
     [java] <ns1:gender>F</ns1:gender>
     [java] <date-of-birth>1975-02-21-05:00</date-of-birth>
     [java] </personal-info>
     [java] <ns1:contact-info>
     [java] <billing-address>
     [java]
            <street>Brandenberg Gate</street>
     [java]
            <street>Suite 1</street>
     [java]
            <city>Berlin</city>
     [java]
            <state>DE</state>
     [java]
            <zip-code>A1B 2C3</zip-code>
     [java]
        </billing-address>
     [java]
        <shipping-address>
     [java]
            <street>456 Champ Elyssee</street>
     [java]
            <street>Unit 1</street>
     [java]
            <city>Paris</city>
     [java]
            <state>FR</state>
     [java]
            <zip-code>12345</zip-code>
     [java]
        </shipping-address>

     [java] <ns1:phone-number type="work">030-(613) 555-1111</ns1:phone-number>
     [java] <ns1:phone-number type="cell">030-(613) 555-2222</ns1:phone-number>
     [java] <ns1:phone-number type="home">(613) 555-3333</ns1:phone-number>
     [java] </ns1:contact-info>
     [java] </ns1:customer>

BUILD SUCCESSFUL

no modifications for Transient are necessary and the addition of phone (613)555-3333 was successful
run from examples folder $TOPLINK\examples\foundation\non-relational\ox

perhaps you can display errors/exceptions you see when exercising this functionality for complex-types
?
Martin Gainty
______________________________________________
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
 
Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.




> From: markus_at_headcrashing.eu
> To: users_at_glassfish.java.net
> Subject: RE: TopLink Essentials: Mixing
> Date: Wed, 23 Mar 2011 20:39:37 +0100
>
> Thanks, it is working well! Noticed that when not setting @Transient, TopLink complains about incorrect types. Seems it loads customers only if it managed to set up the default mapping, which is weird in that case, as I do the mapping because the default doesn't work... Is that wanted?
>
> > -----Original Message-----
> > From: admin_at_java.net [mailto:admin_at_java.net] On Behalf Of
> > forums_at_java.net
> > Sent: Dienstag, 22. März 2011 17:16
> > To: users_at_glassfish.java.net
> > Subject: Re: TopLink Essentials: Mixing
> >
> > The easiest is to add a Descriptor Customizer and add SetMethod,
> > getMethod
> > for that mapping.
> >
> >
> > --
> >
> > [Message sent by forum member 'gyorke']
> >
> > View Post: http://forums.java.net/node/783983
> >
>
>