Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Using a custom collection in EclipseLink

No, converters are for basic mappings, not relationships. (relationships are
more complex)

You could use your get/set methods to do the conversion.


Brennan Spies-3 wrote:
> 
> Would something like this work?
> 
> public class Foo {
>      @OneToMany
>      @Converter(name="StackToList",
> converterClass=StackToListConverter.class)
>      @Convert("StackToList")
>      private Stack<Bar> stack;
> 
>      //etc.
> }
> 
> public class StackToListConverter implements Converter {
>      public Object convertObjectValueToDataValue(Object objectValue,
> Session session) {
>          Stack stack = (Stack) objectValue;
>          return stack.asList();
>      }
> 
>      public Object convertDataValueToObjectValue(Object dataValue, Session
> session) {
>           List list = (List) dataValue;
>           return new Stack(list);
>      }
> }
> 
> On Wed, Nov 23, 2011 at 6:19 AM, James Sutherland
> <jamesssss@xxxxxxxxx>wrote:
> 
>>
>> I would recommend extending Collection, or using the existing Stack class
>> in
>> Java.
>> You could define your own ContainerPolicy to handle your Stack, but
>> probably
>> not the easiest solution.
>>
>> You could also just map it as a List and use get/set methods to wrap the
>> List with your Stack.  That would probably be the easiest solution, and
>> would also support LAZY.
>>
>> See,
>> http://en.wikibooks.org/wiki/Java_Persistence/Basic_Attributes#Conversion
>>
>>
>> Brennan Spies-3 wrote:
>> >
>> > Is there a good example of how to do this (custom collections with
>> EAGER
>> > loading)? My code is like so:
>> >
>> > public interface Stack<E>
>> > {
>> > /**
>> >  * Removes the top of the stack and returns it.
>> >  * @return The item at the top of the stack
>> >  * @throws EmptyStackException If the stack is empty
>> >  */
>> > public E pop() throws EmptyStackException;
>> >  /**
>> >  * Pushes an item onto the stack.
>> >  * @param item The item to push onto the stack
>> >  * @return The item added, or null if item could not be added
>> >  */
>> > public E push(E item);
>> >
>> >         //etc....
>> > }
>> >
>> > The primary implementation simply wraps an ArrayList:
>> >
>> > public class ArrayStack<E> implements Stack<E>, Cloneable
>> > {
>> > private List<E> list;
>> >  public ArrayStack() {
>> > list = new ArrayList<E>();
>> > }
>> >  private ArrayStack(List<E> list) {
>> > this.list = list;
>> > }
>> >
>> >        //etc....
>> > }
>> >
>> > I don't actually want to extend the Collection interface, as that
>> contains
>> > methods I don't want Stack<E> to have.
>> >
>> >
>> > On Wed, Nov 16, 2011 at 10:40 AM, James Sutherland
>> > <jamesssss@xxxxxxxxx>wrote:
>> >
>> >>
>> >> What is it a collection of?
>> >>
>> >> EclipseLink allows collection implementations is you use EAGER on the
>> >> mapping.  LAZY is not supported with implementation because it
>> requires
>> >> the
>> >> interface to allow EclipseLink to use its own lazy collection
>> >> implementation.
>> >>
>> >> I don't think trying to make is an Embeddable is what you want...
>> perhaps
>> >> include your code.
>> >>
>> >>
>> >>
>> >> Brennan Spies-3 wrote:
>> >> >
>> >> > Hi all,
>> >> >
>> >> > I'm a bit new to EclipseLink, and am struggling to figure out a way
>> to
>> >> use
>> >> > a custom collection: a custom Stack interface with a standard
>> >> > implementation, ArrayStack, that simply wraps a java.util.List. As a
>> >> first
>> >> > go, I've specified the ArrayStack as being @Embeddable, and mapped
>> the
>> >> > list
>> >> > directly using @ElementCollection. But EclipseLink needs a
>> >> "targetClass"
>> >> > to
>> >> > do that, but I don't want to specify it in the @Embeddable directly
>> >> since
>> >> > I
>> >> > am using it in more than one entity type.
>> >> >
>> >> > Is there an EclipseLink-specific way to override the "targetClass"
>> on
>> >> this
>> >> > List, or a way to do this using a DescriptorCustomizer?
>> >> >
>> >> >
>> >>
>> >
>> >
>>
> 
> 


-----
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.eclipse.org/forums/index.php?t=thread&frm_id=111&S=1b00bfd151289b297688823a00683aca
EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
Blog:  http://java-persistence-performance.blogspot.com/ Java Persistence
Performance 
-- 
View this message in context: http://old.nabble.com/Using-a-custom-collection-in-EclipseLink-tp32838298p32884747.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top