These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Maintain LinkedHashMap insertion order on @ElementCollection
PostPosted: Tue Nov 02, 2010 4:40 am 
Beginner
Beginner

Joined: Tue Nov 02, 2010 4:29 am
Posts: 21
I'm using an @ElementCollection annotation on a map (implemented with LinkedHashMap) and I want to maintain the insertion order of the keys.
Is there a way of doing this or must I create a wrapper entity for my value objects (BigDecimal) and add an index field?

Reg


Top
 Profile  
 
 Post subject: Re: Maintain LinkedHashMap insertion order on @ElementCollection
PostPosted: Tue Nov 02, 2010 4:58 am 
Beginner
Beginner

Joined: Tue Nov 02, 2010 4:29 am
Posts: 21
If I do have to use a wrapper entity for the BigDecimal what is the best way to manage adding and removing from the collection so that the entities' index fields are updated?


Top
 Profile  
 
 Post subject: Re: Maintain LinkedHashMap insertion order on @ElementCollection
PostPosted: Thu Nov 04, 2010 3:28 pm 
Beginner
Beginner

Joined: Tue Oct 26, 2010 6:12 pm
Posts: 29
Quote:
I want to maintain the insertion order of the keys.


My guess is you would have to store this information in the DB and then while retrieving orderby that column. Essentially simulate the @OrderColumn behavior (which I guess would not work in your case since you use a Map and not a List). So for example:

Code:

        Users u = new Users();
        u.setUsername("foo");
        Map<Integer, Addresses> m = new LinkedHashMap<Integer, Addresses>();
        Addresses addr = new Addresses();
        addr.setCity("Jersey City");
        addr.setZip(07302);
        addr.setPosition(1);
        m.put(addr.getZip(), addr);
        Addresses addr = new Addresses();
        addr.setCity("NYC");
        addr.setZip(11011);
        addr.setPosition(2);       
        m.put(addr.getZip(), addr);
        u.setAddresses(m);



In your User entity:

Code:
    @ElementCollection
    @CollectionTable(name="ADDRESSES", joinColumns={@JoinColumn(name="user_id")})
    @MapKeyColumn(name="ZIP")
    @OrderBy("position asc")
    public Map<Integer, Addresses> getAddresses() {
        return addresses;
    }


Of course, for this to work you'd need the position attribute in the addresses entity.
HTH.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.