Let hql support bitwise and operation

Keywords: Hibernate Java xml Database

Absrtact: at present, hibernate does not support bitwise and operation, and recent projects need such operation again. Fortunately, hibernate provides related extension functions and can realize relevant operation by itself

1, Background

In the work, MySQL is used as the database, java is used as the language of the project, JPA technology is adopted, hibernate is used as the bottom layer, and bit by bit and operation are needed in some projects, but hql does not support it. This paper describes how to make our program support bit by bit and operation

2, Implementation

Preferred implementation of SQLFunction interface

package com.XXXX.hql;
import java.util.List;
import org.hibernate.QueryException;
import org.hibernate.dialect.function.SQLFunction;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.type.Type;

public class BitAndFunction implements SQLFunction {
 @Override

 public boolean hasArguments() {
     return true;
 }

 @Override
 public boolean hasParenthesesIfNoArguments() {
     return true;
 }

 @Override

 public Type getReturnType(Type firstArgumentType, Mapping mapping)
 throws QueryException {
     return org.hibernate.type.IntegerType.INSTANCE;
 }

 @Override
 public String render(Type firstArgumentType, List arguments,SessionFactoryImplementor factory) throws QueryException {
    if(arguments.size() != 2){
      throw new IllegalArgumentException("BitAndFunction requires 2 arguments!"); 
    }
    return arguments.get(0).toString() + " & " + arguments.get(1).toString(); 
 }

}

Then extend the original dialect and register the extended function in hibernate

package com.XXX.hql;
public class CustomSQLDialect extends org.hibernate.dialect.MySQL5InnoDBDialect {
 public CustomSQLDialect() {
     super();
     this.registerFunction("bitand", new BitAndFunction());
 }
}

Finally, to configure dialect is to configure this kind of dialect

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="com.XXX.hql.CustomSQLDialect"/>
           <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
            <property name="hibernate.connection.charSet" value="UTF-8"/>
            <property name="hibernate.show_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

In the hql language, it is used as follows:

from TableEntity where bitand(fieldname,1) =0

bitand is the method of bitwise sum

3, Summary

Hibernate provides flexible extension functions. When a database feature cannot be fully used, you can extend hibernate functions to achieve the corresponding purpose

From: https://my.oschina.net/sunhuaili/blog/315496

Posted by goldages05 on Fri, 01 May 2020 13:39:48 -0700