Agreed. I misunderstood your intentions.
After messing with this for a while I don't think there's any need to use aliases unless you're referencing columns of the same name from different tables - i.e. there's an ambiguity with which columns the 'where clause' is refering to.
Instead of an alias try using a sub-criteria on the joined class - like this:
Code:
List persons = sess.createCriteria(Person.class)
.createCriteria("company")
.add(Restrictions.sqlRestriction("companyName || name like (?)", "%Fritz%", Hibernate.STRING))
.list();
The downside of using sqlRestrictions like this is the coupling with the database column names and lack of portability. For example, the above SQL doesn't work with my MYSQL database. I have to use the concat function instead of ||.
Perhaps a better solution would be to use HQL through the Query API. I'm assuming here that both companyName and name are properties of the Company object:
Code:
Query q = session.createQuery("from Person where company.companyName || company.name like ?");
q.setParameter(0, "%Fritz%");
List persons = q.list();
If name is from Person and companyName from Company then its simply a matter of doing this:
Code:
Query q = session.createQuery("from Person where company.companyName || name like ?");
q.setParameter(0, "%Fritz%");
List persons = q.list();
THe above is more portable because it doesn't reference column names directly. Hibernate changes the property style references to column names. Additionally, with the MYSQL dialect Hibernate changes the || operator to 'concat' in the resulting SQL. Amazing!
Mike