|
well, at least Oracle 8 and 9 doesn't support such a queries, for Oracle 8 see for example: http://www.cs.bris.ac.uk/maintain/OracleDocs/server.816/a76989/express4.htm "You can use a column alias, c_alias, to label the preceding expression in the select list so that the column is displayed with a new heading. The alias effectively renames the select list item for the duration of the query. The alias can be used in the ORDER BY clause, but not other clauses in the query." and in docs for Oracle 9: " ... Specify a different name (alias) for the column expression. Oracle will use this alias in the column heading. The AS keyword is optional. The alias effectively renames the select list item for the duration of the query. The alias can be used in the order_by_clause, but not other clauses in the query." I suppose that subqueries should be used for such a constructions, see Oracle 9 documentation: "Use subqueries for the following purposes:
Postgres does not allow using aliases too: MySQL 3.2.x does not allow aliases in the where clause either: mysql> select id as i from a_table where i = 2; Same problem on SQLServer 2000 FYI The problem only occurs when adding a restriction on the entity ID (PK). session.createCriteria( SynsetType.class ) This one works: - session.createCriteria( SynsetType.class ) This one fails: Passing an alias is useful to recognize the property in a ResultTransformer. An interesting fact is that I don't get the problem when projecting/restricting a subcriteria on the ID, the same way. Oh! As mentionned by shanonvl (Posted: Wed Sep 21, 2005 1:52 pm) http://forum.hibernate.org/viewtopic.php?t=941669 session.createCriteria( SynsetType.class ) This one works: Hibernate: select this_.Synset_Type_Id as y0_ from Synset_Type this_ where this_.Synset_Type_Id=? It sounds logical to me. I'll do as shanonvl suggest, I'll prefix my aliases (disambiguate them). Still no support from Oracle or MySQL (Oracle 10g, mysql 5.0.13rc, hibernate 3.0.5). It`s real pain in ass. workaround have been posted (by francois) and feel free to submit a patch that fixes it. Hello! The query generated by Hibernate is definitely an invalid SQL, since column aliases are not allowed in WHERE clauses. SQL evaluates the statements in the following order: When the WHERE clause is being evaluated, the aliases from the SELECT clause are not yet known. There are several links that describe the problem: http://www.sqlhacks.com/index.php/Retrieve/Alias-in-where I do not have the SQL standard specification at hand, but the third document (at mysql.com) states Please, change the priority of this issue to "Critical". I have a workaround that has worked for me in basic examples. Basically i'm using projections and the alias to bean transformer fo only load selected properties of an entity. // Set up selected columns to load of the entity // Now I can use this without getting the ORA-00904 error Here's the class I wrote to override AliasedProjection. I can't guarantee it works in all cases. It would be great if someone from the Hibernate team can take a quick look and see if there are any potential problems that I've overlooked. /**
/**
/** Default serialization ID. */ /**
/**
} I apologize for the lack of formatting in the above class The patch is against the current 3.2 snapshot. It makes expressions in WHERE clause use column names instead of aliases. Michal, sorry for the delay I would like to add that DB2 doesn't support it either. Also, a note on the workaround. You should only prefix the alias for those columns that are associated with the main table in the query. I found that when adding a prefix to a child table along an association path, an exception was thrown stating that it could not find the associated property. Removing the alias on child table elements allowed me to prevent the use of the alias in the WHERE clause, as well as properly utilize a ResultTransformer. My 2 cents. -B I have simmilar problem with following query (@Hibernate 3.2.4sp1): Everything is ok if there is only one condition either I am new in Hibernate and I will be very thankful if anyone could suggets me some efficient and safe workaround to this. Is this published patch safe in this case? If yes could it have some negative effects on other hibernate components? Chris, I appreciate your help with this issue. I am using the Hibernate AliasToBean results transformer to effectivly lazy load specific properties of my Criteria and Queryies. Essentially adding aliases with the same names as the properties to the criteria and then using the result transformer to only load the properties that are mentioned in the aliases. As such this issue was causing me problems, I made a minor change to your class that fixed an issue where the wrong column was being returned. Here is the fix. /**
Thanks so much for your help Please attach a runnable test case (Java + mapping). I had similar problem but I resolved it using keyword "this" behind the property name in the where clause : Restrictions.like("this.name", value, MatchMode.EXACT) I don't know if it works in all cases... Hi, This bug must be marked as critical, if not, what is the reason for not doing it? maybe the "this. prefix in where"? this works for me but not to others. Please we need a answer. I agree, I can't see how this issue can be minor and still untouched after such a long time. I am having the same problem with SQL Server 2005. Aliases in the where clause can't be used. I am using criteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP) so my aliases are the map's keys. I want the map's keys to be the same as the property names and therefore I have aliases that are the same as the property names. Of course when I add restrictions, I am using property names and this is causing aliases in the where clause. This bug was reported about 3 1/2 YEARS ago. Why hasn't Hibernate fixed this bug? If some databases can handle aliases in the where clause and some can't then the dialects should be updated and the where clause should be created accordingly. Is anyone going to get this thing done? Another fix is proposed in this bug is especially potent because it's the recommended technique for avoiding lazy-loaded properties! http://www.hibernate.org/hib_docs/reference/en/html/performance.html If lazy-loading is the most requested feature, like the documentation says, and if people are expected to use the ProjectionList behavior in criteria searches to avoid loading their blobs, etc... It's crazy this hasn't been fixed. For everything that hibernate does, I'm really surprised someone hasn't addressed this massively basic use case. I shouldn't have to create two tables and two classes because of one simple column. I agree. Its almost 4 years open and still Minor. We've added lots of weird workarounds like SQL groupings, but it seems that Hibernate team keeps ignoring anything related to Criterias. Are criterias dead? Okay, so maybe this won't ever get fixed, the correct workaround to lazy load a property using projections: Create a criteria, build your projection list... Add the columns you want to be loaded on your object, to your projection list. This means I can't ever lazy-load an object using its identity, unless I want to track / set it manually. Will this ever be fixed or is there any reason why this issue will not be fixed? I am wondering, cause this bug was posted over 4 years ago and it is still unassigned. Through this bug QBE in combination with Projection and AliasToBean is not usable. (And i think QBE i one of the greatest features in Hibernate...). Best regards Peter What is the patch about? The changes aren't present in Hibernate core 3.3.2. @Milosz Tylenda Best regards peter patch for Hibernate 3.3.2GA 17882; based on the patch of Milosz Tylenda I ran into this today, using Java DB. I tested interactively against Java DB and MySQL to confirm that aliases didn't work in the WHERE clause, then found the MySQL documentation explaining that the SQL standard does not permit the use of aliases here. http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html I found this discussed in various threads on the Hibernate User forum. Initially I worked around the problem using an SQL restriction, but then I found the advice here: that using "this.property" works. It does, indeed, work for me, but leaves the worry that this (undocumented, so far as I know) behaviour may change in future. I've added a comment to this forum thread: – Justin Forder I said that the "this.property" approach was "(undocumented, so far as I know)". The Hibernate Reference Documentation doesn't mention it (and makes the somewhat misleading statement that "An alias may optionally be assigned to a projection, so that the projected value may be referred to in restrictions or orderings"). However, both "Hibernate in Action" and "Java Persistence with Hibernate" do document the use of this.property as an option. (Section 7.3.3 in "Hibernate in Action", and section 15.1.2 in "Java Persistence with Hibernate".) – Justin Forder |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This is perfectly reasonable SQL. Oracle should support it.