Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » [MOXy] Mapping element to boolean field
[MOXy] Mapping element to boolean field [message #504386] Fri, 18 December 2009 15:45 Go to next message
nikkorv is currently offline nikkorvFriend
Messages: 4
Registered: July 2009
Junior Member
I'm having trouble doing the following ox-mapping:

From xml:
   <BooleanValue />

To this domain object:

class MyObject {
   private boolean booleanValue;

The rules for the mapping is that if the element <BooleanValue /> exist in xml the value of the corresponding field should be set to true, else the value of field booleanValue should be set to false. The reverse logic is also true: booleanValue=true should produce the element and booleanValue=false should omit outputting the element.

I have no clue how to create descriptors for this kind of mapping. Any help is appreciated.
Re: [MOXy] Mapping element to boolean field [message #504427 is a reply to message #504386] Fri, 18 December 2009 21:33 Go to previous messageGo to next message
Blaise Doughan is currently offline Blaise DoughanFriend
Messages: 163
Registered: July 2009
Senior Member

You caught me on the way out the door for the holidays. The following is close to what you could do:

import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.mappings.converters.Converter;
import org.eclipse.persistence.oxm.XMLDescriptor;
import org.eclipse.persistence.oxm.mappings.XMLDirectMapping;
import org.eclipse.persistence.oxm.mappings.nullpolicy.NullPolicy;
import org.eclipse.persistence.sessions.Project;
import org.eclipse.persistence.sessions.Session;

public class CustomerProject extends Project {

    public CustomerProject() {

    private XMLDescriptor getCustomerDescriptor() {
        XMLDescriptor xmlDescriptor = new XMLDescriptor();

        XMLDirectMapping booleanPropertyMapping = new XMLDirectMapping();

        NullPolicy nullPolicy = new NullPolicy();

        booleanPropertyMapping.setConverter(new BooleanConverter());

        return xmlDescriptor;

    private static class BooleanConverter implements Converter {

        public Object convertDataValueToObjectValue(Object dataValue, Session session) {
            return true;

        public Object convertObjectValueToDataValue(Object objectValue, Session session) {
            if(objectValue.equals(true)) {
                return "";
            } else {
                return null;

        public void initialize(DatabaseMapping mapping, Session session) {
            // TODO Auto-generated method stub

        public boolean isMutable() {
            // TODO Auto-generated method stub
            return false;

Re: [MOXy] Mapping element to boolean field [message #504438 is a reply to message #504427] Sat, 19 December 2009 03:42 Go to previous message
nikkorv is currently offline nikkorvFriend
Messages: 4
Registered: July 2009
Junior Member
Thanks for the example, it helped a lot. I have now managed to create a working descriptor for mapping my example object. There is just one annoying thing left though. When marshaling from java to xml i get the following output:


It would be great if the empty booleanVaue-element could be outputted with a trailing slash instead:

   <booleanValue />

I bet there is an easy way to achieve this, I just cant find it in the documentation. Any suggestions?
Previous Topic:Webstart much slower since EL 2.0
Next Topic:Dates: Column Integer X Property Calendar
Goto Forum:

Current Time: Wed Apr 25 16:43:14 GMT 2018

Powered by FUDForum. Page generated in 0.02140 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software