Oracle Java Concat Like 带参数
Oracle Java Concat Like with Arguments
我喜欢在 Oracle SQL 开发人员中执行此查询,以便测试并稍后在 Java
中执行相同的查询。
SELECT
enti.*
FROM
Entity enti
WHERE
enti.apellidouno LIKE CONCAT('%',:apellido1,'%')
AND enti.nombreuno LIKE CONCAT('%',:nombre1,'%')
AND ( :apellido2 IS NULL OR enti.apellidodos LIKE CONCAT('%',:apellido2,'%') )
AND ( :nombre2 IS NULL OR enti.nombredos LIKE CONCAT('%',:nombre2,'%') );
我收到了这条消息
ORA-00909: 论证数量无效
00909.00000 - "invalid number of arguments"
*原因:
*行动:
错误 en la línea: 103, columna: 30
这个位置就是行中CONCAT
小句的字母C
:
enti.apellidouno LIKE
CONCAT
('%',:apellido1,'%')
现在,在我的界面中
public interface EntityRepository extends CrudRepository<Entity, Long>, JpaRepository<Entity, Long> {
public static final String ENTITY_POR_APELLIDOS_Y_NOMBRES
= " SELECT enti "
+ " FROM Entity enti "
+ " WHERE "
+ " enti.apellidouno LIKE CONCAT('%',:apellido1,'%') "
+ " AND enti.nombreuno LIKE CONCAT('%',:nombre1,'%') "
+ " AND (:apellido2 is null OR enti.apellidodos LIKE CONCAT('%',:apellido2,'%') "
+ " AND (:nombre2 is null OR enti.nombredos LIKE CONCAT('%',:nombre2,'%') ";
@Query(ENTITY_POR_APELLIDOS_Y_NOMBRES)
List<Entity> findEntityByApellidosAndNombres(
@Param("apellido1") String apellido1, @Param("apellido2") String apellido2,
@Param("nombre1") String nombre1, @Param("nombre2") String nombre2);
}
在 Java 我得到了:
Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException:
unexpected token: null near line 1, column 353
[ SELECT enti FROM package.Entity enti
WHERE enti.apellidouno LIKE CONCAT('%',:apellido1,'%')
AND enti.nombreuno LIKE CONCAT('%',:nombre1,'%')
AND (:apellido2 is null OR enti.apellidodos LIKE CONCAT('%',:apellido2,'%')
AND (:nombre2 is null OR enti.nombredos LIKE CONCAT('%',:nombre2,'%') ]
您需要嵌套 CONCAT
因为在 Oracle 中它只处理 2 个参数:
SELECT enti.*
FROM Entity enti
WHERE enti.apellidouno LIKE CONCAT(CONCAT('%',:apellido1),'%')
-- ...
您可以选择使用 ||
:
SELECT enti.*
FROM Entity enti
WHERE enti.apellidouno LIKE '%' || :apellido1 || '%'
我喜欢在 Oracle SQL 开发人员中执行此查询,以便测试并稍后在 Java
中执行相同的查询。
SELECT
enti.*
FROM
Entity enti
WHERE
enti.apellidouno LIKE CONCAT('%',:apellido1,'%')
AND enti.nombreuno LIKE CONCAT('%',:nombre1,'%')
AND ( :apellido2 IS NULL OR enti.apellidodos LIKE CONCAT('%',:apellido2,'%') )
AND ( :nombre2 IS NULL OR enti.nombredos LIKE CONCAT('%',:nombre2,'%') );
我收到了这条消息
ORA-00909: 论证数量无效
00909.00000 - "invalid number of arguments"
*原因:
*行动:
错误 en la línea: 103, columna: 30
这个位置就是行中CONCAT
小句的字母C
:
enti.apellidouno LIKE
CONCAT
('%',:apellido1,'%')
现在,在我的界面中
public interface EntityRepository extends CrudRepository<Entity, Long>, JpaRepository<Entity, Long> {
public static final String ENTITY_POR_APELLIDOS_Y_NOMBRES
= " SELECT enti "
+ " FROM Entity enti "
+ " WHERE "
+ " enti.apellidouno LIKE CONCAT('%',:apellido1,'%') "
+ " AND enti.nombreuno LIKE CONCAT('%',:nombre1,'%') "
+ " AND (:apellido2 is null OR enti.apellidodos LIKE CONCAT('%',:apellido2,'%') "
+ " AND (:nombre2 is null OR enti.nombredos LIKE CONCAT('%',:nombre2,'%') ";
@Query(ENTITY_POR_APELLIDOS_Y_NOMBRES)
List<Entity> findEntityByApellidosAndNombres(
@Param("apellido1") String apellido1, @Param("apellido2") String apellido2,
@Param("nombre1") String nombre1, @Param("nombre2") String nombre2);
}
在 Java 我得到了:
Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException:
unexpected token: null near line 1, column 353
[ SELECT enti FROM package.Entity enti
WHERE enti.apellidouno LIKE CONCAT('%',:apellido1,'%')
AND enti.nombreuno LIKE CONCAT('%',:nombre1,'%')
AND (:apellido2 is null OR enti.apellidodos LIKE CONCAT('%',:apellido2,'%')
AND (:nombre2 is null OR enti.nombredos LIKE CONCAT('%',:nombre2,'%') ]
您需要嵌套 CONCAT
因为在 Oracle 中它只处理 2 个参数:
SELECT enti.*
FROM Entity enti
WHERE enti.apellidouno LIKE CONCAT(CONCAT('%',:apellido1),'%')
-- ...
您可以选择使用 ||
:
SELECT enti.*
FROM Entity enti
WHERE enti.apellidouno LIKE '%' || :apellido1 || '%'