Oracle Java Concat与参数类似

时间:2019-07-03 21:18:08

标签: sql oracle sql-like concat

我想在Oracle SQL Developer中执行此查询,以便测试并稍后在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-“参数数量无效” *原因:
*行动: 错误,错误:103,列:30

此位置只是行中C子句的字母CONCAT

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,'%') ]

1 个答案:

答案 0 :(得分:1)

您需要嵌套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 || '%'