Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Embedded collection
PostPosted: Thu Jan 12, 2017 12:25 am 
Newbie

Joined: Thu Jan 12, 2017 12:03 am
Posts: 3
Hi everyone,

When I persist object with embedded collection that is defined like

Code:

   ...
   private List<TimeInstant> values;

   @ElementCollection
   @Column(name = "values")
   public List<TimeInstant> getValues() {
      return values;
   }

   ....


this collection got persisted in random order.
I mean if my list is
Code:
{timeInstant1, timeInstant2, timeInstant3}

in database it may look like
Code:
"values" : [
        { timeInstant2 }, { timeInstant1 }, { timeInstant3 }
]


List is initiated as an ArrayList.
Database is MongoDB
Hibernate version is 5.1.0.Beta3

Is there a way I can get the same order in a database as it is in a list?
Ordering after fetching data is not an option for me.

Thanks.


Top
 Profile  
 
 Post subject: Re: Embedded collection
PostPosted: Thu Jan 12, 2017 9:07 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 278
You can use the annotation @OrderColumn:

Code:
   @ElementCollection
   @OrderColumn(name = "instantIndex")
   private List<TimeInstant> values;


It will change the mapping on the db thougfh, it will look something like:

Code:
        "values" : [
                {
                        "instantIndex" : 0,
                        "timeistant" : "instant1"
                },
                {
                        "instantIndex" : 2,
                        "timeistant" : "instant3"
                },
                {
                        "instantIndex" : 1,
                        "timeistant" : "instant2"
                }
        ]


Does it help?


Top
 Profile  
 
 Post subject: Re: Embedded collection
PostPosted: Thu Jan 12, 2017 3:49 pm 
Newbie

Joined: Thu Jan 12, 2017 12:03 am
Posts: 3
Thanks for your answer.

Unfortunately It will not help - other projects use the same DB and they expect exact order. In particular Scala libraries write collections with respect to original order.

So I was wondering is there a way to achieve it with Hibernare?


Top
 Profile  
 
 Post subject: Re: Embedded collection
PostPosted: Fri Jan 13, 2017 6:44 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 278
I don't think there is a way a the moment but I'll have a look.

Thanks for the feedback, by the way.

Out of curiosity, what Scala library are you using?


Top
 Profile  
 
 Post subject: Re: Embedded collection
PostPosted: Fri Jan 13, 2017 11:55 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 278
I've created an issue to keep track of this: https://hibernate.atlassian.net/browse/OGM-1236


Top
 Profile  
 
 Post subject: Re: Embedded collection
PostPosted: Sun Jan 15, 2017 9:11 pm 
Newbie

Joined: Thu Jan 12, 2017 12:03 am
Posts: 3
on scala side we use casbah and salat

I would really appreciate if you could reflect subject issue in hibernate docs explicitly so other folks could avoid this trap entirely or make adjustments at early stage.

Anyway, many thanks for doing this amazing job. It was a please to work with it even if at the end it didn't fit me.


Top
 Profile  
 
 Post subject: Re: Embedded collection
PostPosted: Mon Jan 16, 2017 6:55 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 278
That's a good suggestion,

Please, let us know if there are some other places where we can improve.

Thanks,
Davide


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