我正在运行以下代码:
Criteria crit = dao.getSesion().createCriteria(TAmbitos.class);
crit.add( Restrictions.sqlRestriction("translate(upper(nombre), 'ÁÉÍÓÚ', 'AEIOU') like translate(upper(?),'ÁÉÍÓÚ', 'AEIOU')",
param+"%", Hibernate.STRING));
crit.add(Restrictions.eq("tipoAmbito", "Empresa"));
crit.setFetchMode("TAmbitosByPais", FetchMode.JOIN);
crit.createAlias("TAmbitosByPais", "TAmbitosByPais", CriteriaSpecification.LEFT_JOIN);
crit.add(Restrictions.eq("TAmbitosByPais", ambito));
这导致ORA-00918: column ambiguously defined
异常。标准有一个表TAmbitos
,它通过TAmbitosByPais
引用自己(任何给定的TAmbitos
行都有一个引用另一个TAmbitosByIdPais
的字段TAmbitos
。生成的SQL是:
SELECT this_.id_ambito AS id1_2_1_, this_.depende_empresa AS depende2_2_1_,
this_.id_zona AS id3_2_1_, this_.pais AS pais2_1_,
this_.id_cls_soc AS id5_2_1_, this_.tipo_ambito AS tipo6_2_1_,
this_.nombre AS nombre2_1_, this_.clave_antigua AS clave8_2_1_,
this_.desactivado AS desactiv9_2_1_,
tambitosby1_.id_ambito AS id1_2_0_,
tambitosby1_.depende_empresa AS depende2_2_0_,
tambitosby1_.id_zona AS id3_2_0_, tambitosby1_.pais AS pais2_0_,
tambitosby1_.id_cls_soc AS id5_2_0_,
tambitosby1_.tipo_ambito AS tipo6_2_0_,
tambitosby1_.nombre AS nombre2_0_,
tambitosby1_.clave_antigua AS clave8_2_0_,
tambitosby1_.desactivado AS desactiv9_2_0_
FROM sac_conf.t_ambitos this_ LEFT OUTER JOIN sac_conf.t_ambitos tambitosby1_
ON this_.pais = tambitosby1_.id_ambito
WHERE TRANSLATE (UPPER (nombre), 'ÁÉÍÓÚ', 'AEIOU') LIKE
TRANSLATE (UPPER (?), 'ÁÉÍÓÚ', 'AEIOU')
AND this_.tipo_ambito = ?
AND this_.pais = ?
正如您在WHERE子句中看到的那样,SQL无法分辨我引用的“nombre”字段。我可以通过在sqlRestriction中添加this_来克服这个问题:
crit.add( Restrictions.sqlRestriction("translate(upper(this_.nombre), 'ÁÉÍÓÚ', 'AEIOU') like translate(upper(?),'ÁÉÍÓÚ', 'AEIOU')",
nombre+"%", Hibernate.STRING));
但是我不知道它是否是最好的解决方案,因为我无法确定SQL是否会始终将查询的表引用为this_。还有另一种定义条件的方法,因此“nombre”引用TAmbitos.nombre
而不是TAmbitosByIdPais.nombre
?
感谢。
答案 0 :(得分:5)
Hibernate Criteria API支持针对此案例的特殊{alias}
占位符:
crit.add( Restrictions.sqlRestriction(
"translate(upper({alias}.nombre), 'ÁÉÍÓÚ', 'AEIOU') like translate(upper(?),'ÁÉÍÓÚ', 'AEIOU')",
param+"%", Hibernate.STRING));
答案 1 :(得分:2)