Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Problem with QueryHints.FETCH and ORDER BY clause
Problem with QueryHints.FETCH and ORDER BY clause [message #1064199] Tue, 18 June 2013 08:41 Go to next message
F C is currently offline F CFriend
Messages: 12
Registered: June 2013
Junior Member
Hi!
just a question.

If I have

@OneToMany(mappedBy="recapito", cascade = {CascadeType.PERSIST,CascadeType.MERGE})
private List<Recapito> r;

and i set
query.setHint(QueryHints.FETCH, "dip.r");

and i execute a query with ORDER BY clause on Recapito attributes, why this sort is not maintained?

Re: Problem with QueryHints.FETCH and ORDER BY clause [message #1064298 is a reply to message #1064199] Tue, 18 June 2013 14:58 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

If you want a relationship to be ordered, you should put the order by on the mapping, not in the query.

With the order by in the query, I would expect the results to be ordered, unless they were already cached.


James : Wiki : Book : Blog : Twitter
Re: Problem with QueryHints.FETCH and ORDER BY clause [message #1064510 is a reply to message #1064298] Wed, 19 June 2013 15:02 Go to previous messageGo to next message
F C is currently offline F CFriend
Messages: 12
Registered: June 2013
Junior Member
So, i added @OrderBy annotation:

@Entity
@Table(name="DIPENDENTE")
public class Dipendente implements Serializable {

............

@ManyToOne
@JoinColumn(name="cRecapito")
private Recapito r;
}

@Entity
@Table(name="RECAPITO")
public class Recapito implements Serializable {

............

//uni-directional many-to-one association to GpfSegment
@ManyToOne
@JoinColumn(name="cComune")
@OrderBy(value="cIstat")
private Comune c;
}

@Entity
@Table(name="COMUNE")
public class Recapito implements Serializable {

............

@Id
@Column(name="cIstat")
private String cIstat;
}

List<Dipendente> is my result but is not order by cIstat...where is the error?
Re: Problem with QueryHints.FETCH and ORDER BY clause [message #1064541 is a reply to message #1064510] Wed, 19 June 2013 19:07 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1039
Registered: July 2009
Senior Member
You show a JoinColumn on a ManyToOne that has nothing to order as it references a single Comune entity. Did you put the orderBy on a ManyToOne or ManyToMany, and how are you reading in the owning entity? Also remember that the orderby only applies if the object is loaded from the database, and it is up to the application to maintain this order. If the application creates the owning entity and doesn't have the collection in order, or changes the order, this is what is presented to the application until it is reloaded from the database.
Previous Topic:InheritancePolicy SingleTableStrategy in Dynamic Persistence
Next Topic:merge problem
Goto Forum:
  


Current Time: Sat Nov 22 13:15:33 GMT 2014

Powered by FUDForum. Page generated in 0.02105 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software