Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Mapping arrays
Mapping arrays [message #488352] Mon, 28 September 2009 07:20 Go to next message
Matti Hansson is currently offline Matti Hansson
Messages: 68
Registered: July 2009
Member
Hello
I'm back with another MOXy question. Is it possible to map an array of primitives to a single node? I try to use XMLCompositeDirectCollectionMapping, but that only seems to work with lists, not arrays. Also, I see there's an ArrayMapping, but no XMLArrayMapping.
Thanks!
Re: Mapping arrays [message #488894 is a reply to message #488352] Wed, 30 September 2009 11:46 Go to previous messageGo to next message
Blaise Doughan is currently offline Blaise Doughan
Messages: 163
Registered: July 2009
Senior Member

Hello Matti,

You can use the same approach I recommended to you on the "MOXy - How to map multiple nodes with the same path?" thread.

http://www.eclipse.org/forums/index.php?t=msg&goto=48888 5&S=561a882ea8c3dad226dbf5eac2f92d5b#msg_488885

Except instead of using an XMLCompositeObjectMapping you can use an XMLCompositeDirectCollectionMapping. In this case you would make use of a List as your intermediate object.

I hope this helps.

-Blaise
Re: Mapping arrays [message #491345 is a reply to message #488894] Wed, 14 October 2009 04:43 Go to previous messageGo to next message
Matti Hansson is currently offline Matti Hansson
Messages: 68
Registered: July 2009
Member
Thank you for the response Blaise, but I find your method does not work. Through debugging I discovered that the converter is applied to each element in the collection, rather than the collection itself.

If I use an array, the mapping tries to create an iterator and casts the value to a Collection, but since the object is an array it fails and throws an exception.

Is there another way?
Thanks!

EDIT: Nevermind. Found I could use
mapping.setAttributeElementClass(double[].class);
with the XMLDirectCollectionMapping and no converter.

EDIT: Scratch that. I had introduced a Vector which I had forgotten about. Mapping to arrays still doesn't work Sad

[Updated on: Wed, 14 October 2009 07:39]

Report message to a moderator

Re: Mapping arrays [message #491471 is a reply to message #488352] Wed, 14 October 2009 13:57 Go to previous messageGo to next message
Blaise Doughan is currently offline Blaise Doughan
Messages: 163
Registered: July 2009
Senior Member

Hi Matti,

You could solve this by writing a custom AttributeAccessor. Below is an example of what it could look like.

XMLCompositeDirectCollectionMapping valueMapping = new XMLCompositeDirectCollectionMapping();
ArrayAttributeAccessor<String> arrayAttributeAccessor = new ArrayAttributeAccessor<String>(String.class);
arrayAttributeAccessor.setAttributeName("value");
valueMapping.setAttributeAccessor(arrayAttributeAccessor);
valueMapping.setXPath("valuePartB/text()");
xmlDescriptor.addMapping(valueMapping);


import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Vector;

import org.eclipse.persistence.exceptions.DescriptorException;
import org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor;

public class ArrayAttributeAccessor<T> extends InstanceVariableAttributeAccessor {

    private Class<T> itemType;
    
    public ArrayAttributeAccessor(Class<T> itemType) {
        this.itemType = itemType;
    }
    
    @Override
    public Object getAttributeValueFromObject(Object object) throws DescriptorException {
        T[] array = (T[]) super.getAttributeValueFromObject(object);
        Vector<T> vector = new Vector<T>(array.length);
        for(T t : array) {
            vector.add(t);
        }
        return vector;
    }

    @Override
    public void setAttributeValueInObject(Object object, Object value) throws DescriptorException {
        Vector<T> vector = (Vector<T>) value;
        T[] array = (T[]) Array.newInstance(itemType, vector.size());
        vector.toArray(array);
        super.setAttributeValueInObject(object, array);
    }

}


-Blaise
Re: Mapping arrays [message #491612 is a reply to message #491471] Thu, 15 October 2009 05:22 Go to previous message
Matti Hansson is currently offline Matti Hansson
Messages: 68
Registered: July 2009
Member
Yes, that worked perfectly!
Thanks a lot!
Previous Topic:UnitOfWork events
Next Topic:MOXy - How to map multiple nodes with the same path?
Goto Forum:
  


Current Time: Sat Jul 26 01:18:25 EDT 2014

Powered by FUDForum. Page generated in 0.27128 seconds