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: OrderBy annotation and persisting a list
PostPosted: Wed Sep 26, 2007 8:34 am 
Newbie

Joined: Tue May 29, 2007 6:23 pm
Posts: 11
Hi to all;
This will be a newbie question, I have a collection mapped like the following:

Code:
class A {
....
@OneToMany(cascade={CascadeType.PERSIST, CascadeType.REMOVE}, mappedBy="a")
@OrderBy("id")
List<B> listOfBs = new ArrayList<B>();

void addB(B b) {
  b.setA(this);
  listOfBs.add(b);
}
...
}

class B {
...
A a;
String name;
B(name) {this.name=name;}
...
}
A newA = new A();
a.addB(new B("1"));
a.addB(new B("2"));
a.addB(new B("3"));
a.addB(new B("4"));

The question is in what order the B instances are saved, is it guaranteed to have the firstly added B to be persisted first? What if CollectionOfElements is used, is OrderBy annotation enough to guarantee the addition order, or do I have to use IndexColumn annotation and list semantics?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 26, 2007 11:04 am 
Beginner
Beginner

Joined: Tue Jan 18, 2005 9:37 am
Posts: 29
Location: The Netherlands
Hi Akemi,

As far as I know OrderBy does NOT guarantee the order in which elements are saved, but only the sequence in which elements are loaded. This is translated into an orderby clause in the select query.

In reality the ordering will be the same because when an A entity is saved, the cascade feature will iteratate over all B's and save each of them if they are not saved already.

Regards,
Bas


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 26, 2007 4:15 pm 
Newbie

Joined: Tue May 29, 2007 6:23 pm
Posts: 11
thanks for the reply,
in fact I have come up with this question after the nondeterministic behaviour when saving a collection of primitive type. The situation is as follows:
@CollectionOfElements
@JoinTable(
table=@Table(name="TABLE_B"),
joinColumns = @JoinColumn(name="A_Id")
)
@Column(name="name")
List<Short> listOfBs = new ArrayList<Short>();

Well, this mapping just does not persist (or load) the collection in the order of insertion of Short elements. Just having a unit test, the test fails from time to time, the test simply fills the collection with some values then persists the entity, then read the persisted entity and compare with the values set for the elements before persist operation. The element at a determined index for the collection is different before and after persist operation. So I wonder if it is a persist or a load problem. Since the behaviour is nondeterministic, I cannot reproduce the condition, and after the test all work is undone to be able to run the test once more, so I cannot see the database order for the elements.


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.