Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

Problema de tipo de caractere Oracle na consulta Hibernate HQL


Resolvi esse problema usando a interface OraclePreparedStatement e Hibernate UserType.

Criou uma nova classe UserType estendendo a interface org.hibernate.usertype.UserType e implementou os métodos nullSafeSet(), nullSafeGet() .

nullSafeSet(), temos o primeiro parâmetro como PreparedStatement, dentro do método eu converti PreparedStatement no objeto OraclePreparedStatement e passei o valor String usando o método setFixedCHAR().

Aqui está o código completo da classe impl UserType.
package nc3.jws.persistence.userType;

import java.io.Serializable;
import java.sql.PreparedStatement; 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.apache.commons.lang.StringUtils;
import org.hibernate.type.StringType;
import org.hibernate.usertype.UserType;

/**
* 
* based on www.hibernate.org/388.html
*/

 public class OracleFixedLengthCharType implements UserType {

public OracleFixedLengthCharType() {
    System.out.println("OracleFixedLengthCharType constructor");
}

public int[] sqlTypes() {
    return new int[] { Types.CHAR };
}



public Class<String> returnedClass() {
    return String.class;
}

public boolean equals(Object x, Object y) {
    return (x == y) || (x != null && y != null && (x.equals(y)));
}

@SuppressWarnings("deprecation")
public Object nullSafeGet(ResultSet inResultSet, String[] names, Object o) throws SQLException {
    //String val = (String) Hibernate.STRING.nullSafeGet(inResultSet, names[0]);
    String val = StringType.INSTANCE.nullSafeGet(inResultSet, names[0]);
    //System.out.println("From nullSafeGet method valu is "+val);
    return val == null ? null : StringUtils.trim(val);
}


public void nullSafeSet(PreparedStatement inPreparedStatement, Object o,
        int i)
                throws SQLException {

    String val = (String) o;
    //Get the delegatingStmt object from DBCP connection pool PreparedStatement object.
    org.apache.commons.dbcp.DelegatingStatement delgatingStmt = (org.apache.commons.dbcp.DelegatingStatement)inPreparedStatement;
    //Get OraclePreparedStatement object using deletatingStatement object.

    oracle.jdbc.driver.OraclePreparedStatement oraclePreparedStmpt = (oracle.jdbc.driver.OraclePreparedStatement)delgatingStmt.getInnermostDelegate();
    //Call setFixedCHAR method, by passing string type value .
    oraclePreparedStmpt.setFixedCHAR(i, val);
}


public Object deepCopy(Object o) {
    if (o == null) {
        return null;
    }
    return new String(((String) o));
}


public boolean isMutable() {
    return false;
}

public Object assemble(Serializable cached, Object owner) {
    return cached;
}


public Serializable disassemble(Object value) {
    return (Serializable) value;
}

public Object replace(Object original, Object target, Object owner) {
    return original;
}

public int hashCode(Object obj) {
    return obj.hashCode();
}

  }

Configurei esta classe com a anotação @TypeDefs na classe Entity.
@TypeDefs({
@TypeDef(name = "fixedLengthChar", typeClass = nc3.jws.persistence.userType.OracleFixedLengthCharType.class)

})

Adicionado este tipo às colunas do tipo CHAR
@Type(type="fixedLengthChar")
@Column(name="SERVICE_NAME",nullable=true,length=16)
public String getServiceName() {
    return this.serviceName;
}