Trying to construct in code a Restrictions.sqlRestriction of the form:
Restrictions.sqlRestriction("upper({alias}." + fieldName + ") > Smith");
where the
fieldName variable contains a property name.
This works fine so long as the property name is identical to the column name. If not, I get errors such as the one listed below. For a general solution,
fieldname needs to be
columnName.
My questions then are: (1) is my understanding of sqlRestriction's needs correct and if so (2) what is the proper way to retrieve the column name for the desired property name?
What I have so far is:
Code:
private String getColumnName(String fieldName) {
SessionFactory pmf = session.getSessionFactory();
ClassMetadata meta = pmf.getClassMetadata(Corporate.class);
String[] cols = ((BasicEntityPersister)meta).toColumns(fieldName);
return cols[0];
}
which seems to work.
Am I on the right path? Is there a better way to do this? Are there any problems with this approach?
Thanks,
Gerald
Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelpHibernate version: 3.0 Mapping documents:
<property
name="corpName"
column="CORP_NAME"
/>
Full stack trace of any exception that occurs:
Hibernate: select this_.CORPORATE_ID as CORPORATE1_0_, this_.CITY as CITY1_0_, this_.CORP_NAME as CORP3_1_0_, this_.HQ_ADDRESS1 as HQ4_1_0_, this_.HQ_ADDRESS2 as HQ5_1_0_, this_.STATE as STATE1_0_, this_.TAX_NUMBER_FED as TAX7_1_0_, this_.TAX_NUMBER_STATE as TAX8_1_0_, this_.ZIP as ZIP1_0_ from CORPORATE this_ where (lower(this_.corpName) = 'intel') order by this_.CORP_NAME asc
2005-04-14 10:29:34,360 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 904, SQLState: 42000
2005-04-14 10:29:34,360 [main] ERROR org.hibernate.util.JDBCExceptionReporter - ORA-00904: "THIS_"."CORPNAME": invalid identifier
Name and version of the database you are using: Oracle10gCode: