sql >> Databáze >  >> RDS >> Oracle

Problém s typem znaku Oracle v dotazu Hibernate HQL

Tento problém jsem vyřešil pomocí rozhraní OraclePreparedStatement a Hibernate UserType.

Vytvořila novou třídu UserType rozšířením rozhraní org.hibernate.usertype.UserType a poskytla implementaci pro metody nullSafeSet(), nullSafeGet() .

Metoda nullSafeSet(), máme první parametr jako PreparedStatement, uvnitř metody jsem přetypoval PreparedStatement do objektu OraclePreparedStatement a předal hodnotu String pomocí metody setFixedCHAR().

Zde je úplný kód třídy 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();
}

  }

Tuto třídu jsem nakonfiguroval s anotací @TypeDefs ve třídě Entity.

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

})

Tento typ byl přidán do sloupců typu CHAR

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


  1. Průvodce návrhem databáze pro události kalendáře a připomenutí v MySQL

  2. Proč dotaz SELECT nespustí transakci? [Věštec]

  3. CodeIgniter - Seskupit podle objednávky tím, že nefunguje podle očekávání

  4. problém s python manage.py migrate -> Žádný modul s názvem psycopg2