Oracle
 sql >> Database >  >> RDS >> Oracle

Problema di tipo char Oracle nella query Hibernate HQL

Ho risolto questo problema utilizzando l'interfaccia OraclePreparedStatement e Hibernate UserType.

Creata una nuova classe UserType estendendo l'interfaccia org.hibernate.usertype.UserType e fornendo l'implementazione per i metodi nullSafeSet(), nullSafeGet() .

nullSafeSet(), abbiamo il primo parametro come PreparedStatement, all'interno del metodo ho eseguito il cast di PreparedStatement nell'oggetto OraclePreparedStatement e ho passato il valore String usando il metodo setFixedCHAR().

Ecco il codice completo della classe UserType impl.

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();
}

  }

Configurata questa classe con l'annotazione @TypeDefs nella classe Entity.

@TypeDefs({
@TypeDef(name = "fixedLengthChar", typeClass = nc3.jws.persistence.userType.OracleFixedLengthCharType.class)

})

Aggiunto questo tipo alle colonne di tipo CHAR

@Type(type="fixedLengthChar")
@Column(name="SERVICE_NAME",nullable=true,length=16)
public String getServiceName() {
    return this.serviceName;
}