JPA2: Case-insensitive like matching anywhere

后端 未结 7 2025
梦毁少年i
梦毁少年i 2020-12-13 03:45

I have been using Hibernate Restrictions in JPA 1.0 ( Hibernate driver ). There is defined Restrictions.ilike(\"column\",\"keyword\", MatchMode.ANYWHERE) which

7条回答
  •  再見小時候
    2020-12-13 04:19

    Easier and more efficient to enforce case insensitity within the database than JPA.

    1. Under the SQL 2003, 2006, 2008 standards, can do this by adding COLLATE SQL_Latin1_General_CP1_CI_AS OR COLLATE latin1_general_cs to the following:

      • Column Definition

        CREATE TABLE  (
            [DEFAULT...] 
                                    [NOT NULL|UNIQUE|PRIMARY KEY|REFERENCES...]
                                    [COLLATE ], 
          ...
        )
        
        
      • Domain Definition

        CREATE DOMAIN  [ AS ] 
          [ DEFAULT ... ] [ CHECK ... ] [ COLLATE  ]
        
      • Character Set Definition

        CREATE CHARACTER SET 
        [ AS ] GET  [ COLLATE  ]
        
      • For full description of above refer: http://savage.net.au/SQL/sql-2003-2.bnf.html#column%20definition http://dev.mysql.com/doc/refman/5.1/en/charset-table.html http://msdn.microsoft.com/en-us/library/ms184391.aspx

      • In Oracle, can set NLS Session/Configuration parameters

         SQL> ALTER SESSION SET NLS_COMP=LINGUISTIC;
         SQL> ALTER SESSION SET NLS_SORT=BINARY_CI;
         SQL> SELECT ename FROM emp1 WHERE ename LIKE 'McC%e';
        
         ENAME
         ----------------------
         McCoye
         Mccathye
        

        Or, in init.ora (or OS-specific name for initialization parameter file):

        NLS_COMP=LINGUISTIC
        NLS_SORT=BINARY_CI
        

        Binary sorts can be case-insensitive or accent-insensitive. When you specify BINARY_CI as a value for NLS_SORT, it designates a sort that is accent-sensitive and case-insensitive. BINARY_AI designates an accent-insensitive and case-insensitive binary sort. You may want to use a binary sort if the binary sort order of the character set is appropriate for the character set you are using. Use the NLS_SORT session parameter to specify a case-insensitive or accent-insensitive sort:

        Append _CI to a sort name for a case-insensitive sort.
        Append _AI to a sort name for an accent-insensitive and case-insensitive sort. 
        

        For example, you can set NLS_SORT to the following types of values:

        FRENCH_M_AI
        XGERMAN_CI
        

        Setting NLS_SORT to anything other than BINARY [with optional _CI or _AI] causes a sort to use a full table scan, regardless of the path chosen by the optimizer. BINARY is the exception because indexes are built according to a binary order of keys. Thus the optimizer can use an index to satisfy the ORDER BY clause when NLS_SORT is set to BINARY. If NLS_SORT is set to any linguistic sort, the optimizer must include a full table scan and a full sort in the execution plan.

        Or, if NLS_COMP is set to LINGUISTIC, as above, then sort settings can be applied locally to indexed columns, rather than globally across the database:

        CREATE INDEX emp_ci_index ON emp (NLSSORT(emp_name, 'NLS_SORT=BINARY_CI'));
        

        Reference: ORA 11g Linguistic Sorting and String Searching ORA 11g Setting Up a Globalization Support Environment

      • 提交回复
        热议问题