String filteringTag = "habr" ; Session session = ... ; Criteria criteria = session. createCriteria ( StringArrayContainer. class ) ; criteria. add ( new StringArrayContainsCriterion ( "tags" , filteringTag ) ) ; List containers = criteria. list ( ) ; // , "habr" ...
String filteringTag = "habr" ; Session session = ... ; Criteria criteria = session. createCriteria ( StringArrayContainer. class ) ; criteria. add ( new StringArrayContainsCriterion ( "tags" , filteringTag ) ) ; List containers = criteria. list ( ) ; // , "habr" ...
String filteringTag = "habr" ; Session session = ... ; Criteria criteria = session. createCriteria ( StringArrayContainer. class ) ; criteria. add ( new StringArrayContainsCriterion ( "tags" , filteringTag ) ) ; List containers = criteria. list ( ) ; // , "habr" ...
String filteringTag = "habr" ; Session session = ... ; Criteria criteria = session. createCriteria ( StringArrayContainer. class ) ; criteria. add ( new StringArrayContainsCriterion ( "tags" , filteringTag ) ) ; List containers = criteria. list ( ) ; // , "habr" ...
String filteringTag = "habr" ; Session session = ... ; Criteria criteria = session. createCriteria ( StringArrayContainer. class ) ; criteria. add ( new StringArrayContainsCriterion ( "tags" , filteringTag ) ) ; List containers = criteria. list ( ) ; // , "habr" ...
String filteringTag = "habr" ; Session session = ... ; Criteria criteria = session. createCriteria ( StringArrayContainer. class ) ; criteria. add ( new StringArrayContainsCriterion ( "tags" , filteringTag ) ) ; List containers = criteria. list ( ) ; // , "habr" ...
- @Override
- public Object nullSafeGet ( ResultSet rs, String [ ] names, Object owner ) throws HibernateException, SQLException {
- String value = ( String ) Hibernate. TEXT . nullSafeGet ( rs, names [ 0 ] ) ;
- if ( value == null ) {
- return null ;
- } else {
- String [ ] array = StringUtils. split ( value. trim ( ) , '' ) ;
- for ( int i = 0 ; i < array. length ; i ++ ) {
- array [ i ] = WhitespaceEscapeUtil. unescape ( array [ i ] ) ;
- }
- return array ;
- }
- }
- @Override
- public void nullSafeSet ( PreparedStatement st, Object value, int index ) throws HibernateException, SQLException {
- if ( value == null ) {
- Hibernate. TEXT . nullSafeSet ( st, null , index ) ;
- } else {
- String [ ] array = ( String [ ] ) value ;
- String [ ] copy = new String [ array. length ] ;
- for ( int i = 0 ; i < array. length ; i ++ ) {
- copy [ i ] = WhitespaceEscapeUtil. escape ( array [ i ] ) ;
- }
- Hibernate. TEXT . nullSafeSet ( st, '' + StringUtils. join ( copy, '' ) + '' , index ) ;
- }
- }
It is clear that the implementation through like is not very efficient in terms of performance. In order to optimize, we will write the same thing, using the capabilities of PostgreSQL 8.x.
- public class GenericStringArrayContainsCriterion extends LikeExpression {
- public GenericStringArrayContainsCriterion ( String propertyName, String value ) {
- super ( propertyName, "%" + WhitespaceEscapeUtil. escape ( value ) + "%" )
- }
- }
And using the “strategy” template, we will choose the implementation depending on the current dialect:
- public class PostgresStringArrayContainsCriterion implements Criterion {
- private final String propertyName ;
- private final String value ;
- private final String TEMPLATE = "to_tsvector ('simple', {column}) @@ plainto_tsquery ('simple',?)" ;
- private static final char TAG_SEPARATOR = '' ;
- public PostgresStringArrayContainsCriterion ( String propertyName, String value ) {
- this . propertyName = propertyName ;
- this . value = WhitespaceEscapeUtil. escape ( value ) ;
- }
- public String toSqlString ( Criteria criteria, CriteriaQuery criteriaQuery ) throws HibernateException {
- return StringHelper. replace ( TEMPLATE, "{column}" , criteriaQuery. getColumn ( criteria, propertyName ) ) ;
- }
- public TypedValue [ ] getTypedValues ( Criteria criteria, CriteriaQuery criteriaQuery ) throws HibernateException {
- return new TypedValue [ ] { new TypedValue ( Hibernate. STRING , value, EntityMode. POJO ) } ;
- }
- }
- public class StringArrayContainsCriterion implements Criterion {
- private final String propertyName ;
- private final String value ;
- public StringArrayContainsCriterion ( String propertyName, String value ) {
- this . propertyName = propertyName ;
- this . value = value ;
- }
- public String toSqlString ( Criteria criteria, CriteriaQuery criteriaQuery ) throws HibernateException {
- return getStrategy ( criteriaQuery. getFactory ( ) . getDialect ( ) ) . toSqlString ( criteria, criteriaQuery ) ;
- }
- public TypedValue [ ] getTypedValues ( Criteria criteria, CriteriaQuery criteriaQuery ) throws HibernateException {
- return getStrategy ( criteriaQuery. getFactory ( ) . getDialect ( ) ) . getTypedValues ( criteria, criteriaQuery ) ;
- }
- public Criterion getStrategy ( Dialect dialect ) {
- if ( dialect instanceof PostgreSQLDialect ) {
- return new PostgresStringArrayContainsCriterion ( propertyName, value ) ;
- } else {
- return new GenericStringArrayContainsCriterion ( propertyName, value ) ;
- }
- }
- }
Source: https://habr.com/ru/post/91434/
All Articles