Domanda

I have the following classes (simplified for clarity):

Class Top {
  InternationalStringType name;
}

Class InternationalStringType {
  List<LocalizedStringType> localizedString; 
}

Class LocalizedStringType {
  String value;
}

The following EJBQL query successfully retrieves all instance of Top with their sub-objects populated:

SELECT DISTINCT Object(t) FROM Top t LEFT OUTER JOIN t.name nm LEFT OUTER JOIN nm.localizedString nm_ls

I would like to modify the above query such that the results are sorted by Top.name.localizedString.value

What is the correct syntax to do this? I tried the following query but I get "ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list"

SELECT DISTINCT Object(t) FROM Top t LEFT OUTER JOIN t.name nm LEFT OUTER JOIN nm.localizedString nm_ls ORDER BY nm_ls.value ASC

Its not clear to me what to put in the select list for the ORDER BY clause.

Following give errors for the "," after Object(t) no matter what I put after the comma:

SELECT DISTINCT Object(t), Object(nm_ls) FROM Top t LEFT OUTER JOIN t.name nm LEFT OUTER JOIN nm.localizedString nm_ls ORDER BY nm_ls.value ASC

In case its relevant my JPA implementation is hibernate 3.6.4.Final. TIA for your help.

È stato utile?

Soluzione 2

I was able to make the ORDER BY clause work with my query by simply removing the DISTINCT qualifier in the SELECT clause. I am not sure I understand the fine points but the following modified query worked perfectly!

SELECT t FROM Top t LEFT JOIN t.name nm LEFT JOIN nm.localizedString nm_ls ORDER BY nm_ls.value ASC

I now get a result set with Top instances sorted by the Top.name.localizedString.value attribute as I was hoping for.

Now if any one can explain why DISTINCT does not work that would be great. Thanks.

Altri suggerimenti

The fields used in the ORDER BY clause must be CMP fields, they cannot be entity identifiers or CMR fields. In addition, you must be careful which CMP fields you specify in the ORDER BY clause. If the query selects a collection of entities, the ORDER BY clause can be used only with CMP fields of the entity type selected. For example the following query is illegal, because the CMP field used in the ORDER BY clause is not a field of the entity type selected.

SELECT OBJECT( c ) FROM Customer AS c ORDER BY c.address.city

Because the city CMP field is not a direct CMP field of the Customer EJB, you cannot use it in the ORDER BY clause. The only CMP fields you can use in the ORDER BY clause are those that are direct CMP fields of the entity type being selected. This is an unreasonable restriction.

A similar restriction applies to CMP results. The CMP field used in the ORDER BY clause must be the same as the CMP field identified in the SELECT clause. For example, the following query is illegal, because the CMP identified in the SELECT clause differs from the one used in the ORDER BY clause.

SELECT c.address.city FROM Customer AS c ORDER BY c.address.state

In the above query, we wanted a list of all the cities, ordered by state. Unfortunately, this is illegal. You can't order by the state CMP field if you are selecting using the city CMP field.

I've heard that this second restriction may have been forced on us by the limitations of one of the major RDBS systems, which cannot order by any columns not present in the select clause - remember that EJB QL is compiled into a native query language, which in RDBS is SQL. The limitations of major vendors will be a factor in creating any abstraction - the least common denominator will prevail.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top