I join 2 tables on join condition:
table1.col1=table2.col2
col1 is NOT table1's PK, col2 is table1's PK
col1's type is CHAR(20), col2's type is VARCHAR(40)
I use Hibernate's Criteria API and it doesn't recognise the join condition and gives me 0 row.
I've trimmed the padding spaces by extending UserType but it doesn't work. Seems that it only works for retrieving a CHAR field, not joining it to another field.
Code:
Code:
Criteria pq = session.createCriteria(Devices.class).createAlias("PrisProduct", "pp");
pq.add(Restrictions.eq(field, value.trim()));
Collection pc = pq.list();
The Devices.hbm.xml excerpt:
Code:
<property name="dieCode" type="com.st.wma.datalayer.hibernate.util.OracleCHAR">
<column name="DIE_CODE" length="20" not-null="true" unique="true" />
</property>
<many-to-one name="PrisProduct" class="com.st.wma.datalayer.hibernate.model.PrisProduct" column="DIE_CODE" not-null="true" insert="false" update="false">
</many-to-one>
The PrisProduct.hbm.xml excerpt:
Code:
<set name="devices" table="DEVICES">
<key column="CAM_PRODUCT"/>
<one-to-many class="com.st.wma.datalayer.hibernate.model.Devices"/>
</set>
OracleCHAR code is
Code:
package com.st.wma.datalayer.hibernate.util;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.driver.OracleTypes;
import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
/**
* This class trim value of Oracle CHAR columns that causes problem of padding
* with spaces.
*/
public class OracleCHAR implements UserType {
public OracleCHAR() {
super();
}
public int[] sqlTypes() {
return new int[]{OracleTypes.FIXED_CHAR};
}
public Class<String> returnedClass() {
return String.class;
}
public boolean equals(Object x, Object y) throws HibernateException {
return (x == y) || (x != null && y != null && (x.equals(y)));
}
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
String val = rs.getString(names[0]);
if (null == val) {
return null;
}
else {
//String trimmed = StringUtils.stripEnd(val, " ");
String trimmed = StringUtils.trim(val);
if (trimmed.equals("")) {
return null;
}
else {
return trimmed;
}
}
}
public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {
if (value == null) {
st.setNull(index, OracleTypes.FIXED_CHAR);
}
else {
st.setObject(index, value, OracleTypes.FIXED_CHAR);
}
}
public Object deepCopy(Object value) throws HibernateException {
if (value == null)
return null;
return new String((String) value);
}
public boolean isMutable() {
return false;
}
public Object assemble(Serializable arg0, Object arg1)
throws HibernateException {
return null;
}
public Serializable disassemble(Object arg0) throws HibernateException {
return null;
}
public int hashCode(Object arg0) throws HibernateException {
return 0;
}
public Object replace(Object arg0, Object arg1, Object arg2)
throws HibernateException {
return null;
}
}
It's not in my capacity to change the DB schema so I can't change the CHAR to VARCHAR.
environment: Windows XP, JDK 1.6.0_07, Hibernate 3, Oracle 10g
Thanks!