Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Order of items in array
PostPosted: Fri Jan 13, 2017 4:28 pm 
Newbie

Joined: Fri Jan 13, 2017 4:16 pm
Posts: 6
Hello guys,

I am trying to store documents with location in GeoJSON format
Code:
....
"location" : {
        "type" : "Point",
        "coordinates" : [longitude, latitude]
}
...


However, after data are persisted, in SOME cases order of these coordinates is not kept. The problem is, that I am creating geospatial "2dsphere" index on "location" field and if order is not preserved, I can't use geo queries at all :(.
From what I have read so far, mongo driver should keep order of items within array during persistence. Am I missing something or ?

Java code of GeoJSON object in my application:
Code:
@Embeddable
public class GeoJSON {

    /**
     * Geo object type
     */
    private String type;

    /**
     * List of coordinates [lon, lat]
     */
    @ElementCollection(fetch = FetchType.EAGER)
    private List<Double> coordinates;
}


Thanks in advance!


Top
 Profile  
 
 Post subject: Re: Order of items in array
PostPosted: Mon Jan 16, 2017 6:50 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 279
Hi, see this post: viewtopic.php?t=1043903

Wouldn't it be better to store the coordinates as longitude and latitude instead of a list?

Anyway, at the moment, if you want to be sure you need to use the OrderColumn annotation but this will change the underlying mapping.

I've created an issue for this problem an dyou will find all the details in the post I linked.

Thanks,
Davide


Top
 Profile  
 
 Post subject: Re: Order of items in array
PostPosted: Mon Jan 16, 2017 5:53 pm 
Newbie

Joined: Fri Jan 13, 2017 4:16 pm
Posts: 6
Thank you DavideD for quick response and sorry for creating duplicate thread. I had to miss that one :(!

Well, the main reason I store them as a collection is that I want to query them using geo functions (for example to find closest "documents" to specific point on Earth. So the query would be like).
Code:
collection().find({"location" : { $near : { $geometry : { type : "Point" , coordinates : [lat, lon]}, $maxDistance : 500}}})


I am quite new to Hibernate and MongoDB, so please feel free to correct me if there is a better way to do that.


Top
 Profile  
 
 Post subject: Re: Order of items in array
PostPosted: Tue Jan 17, 2017 6:52 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 279
No, I think that's the way you are supposed to work with coordinates in MongoDB.

The fact that you cannot rely on the order of arrays is a bug in OGM.

I'll try to fix for the next release.

Thanks for the feedback,
Davide


Top
 Profile  
 
 Post subject: Re: Order of items in array
PostPosted: Tue Feb 28, 2017 12:52 pm 
Newbie

Joined: Fri Jan 13, 2017 4:16 pm
Posts: 6
Thanks for the fix Davide!
Storing of arrays now works fine and it keeps the order of items.(5.1 CR 1) However I found out that when fetching data from database, sometimes order of items is not kept.


Top
 Profile  
 
 Post subject: Re: Order of items in array
PostPosted: Wed Mar 01, 2017 11:15 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 279
Can you show me an example of when the order is not kept, please?


Top
 Profile  
 
 Post subject: Re: Order of items in array
PostPosted: Mon Mar 06, 2017 8:42 am 
Newbie

Joined: Fri Jan 13, 2017 4:16 pm
Posts: 6
It is the same use case as above. When I fetch locations from DB, longitude and latitude are sometimes in reversed order. I checked them directly in database using Robomongo and they are stored properly.


Top
 Profile  
 
 Post subject: Re: Order of items in array
PostPosted: Tue Mar 07, 2017 5:37 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 279
Hi, I'll have a look again.

If you could provide a testcase it would help me a lot, though.

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 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.