-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Joining fixed-length CHAR field with VARCHAR field
PostPosted: Tue Jul 06, 2010 2:58 am 
Newbie

Joined: Thu Jul 01, 2010 4:36 am
Posts: 4
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!


Top
 Profile  
 
 Post subject: Re: Joining fixed-length CHAR field with VARCHAR field
PostPosted: Tue Jul 06, 2010 10:27 pm 
Newbie

Joined: Thu Jul 01, 2010 4:36 am
Posts: 4
Reply from Gail Badner at http://opensource.atlassian.com/project ... e/HHH-5354:

Quote:
You probably need to cast one of the columns the same type as the other column.


Top
 Profile  
 
 Post subject: Re: Joining fixed-length CHAR field with VARCHAR field
PostPosted: Sun Jul 11, 2010 10:44 pm 
Newbie

Joined: Thu Jul 01, 2010 4:36 am
Posts: 4
As you can see in the attached code, the returned class of type OracleCHAR is String, the same as the other field. So I think casting is not necessary.

I'd turned on <show_sql> in hibernate.cfg.xml:

Code:
    from
        MAPS this_
    inner join
        WAFERS w1_
            on this_.WAFER_ID=w1_.WAFER_ID
    inner join
        DEVICES d2_
            on this_.DIE_ID=d2_.DIE_ID
    inner join
        PRIS_PRODUCT PP3_
            on trim(d2_.DIE_CODE)=pp3_.CAM_PRODUCT
    left outer join
        ANALYSIS_RESULTS mapanalysi8_
            on this_.MAP_ID=mapanalysi8_.MAP_
ID
    WHERE
        THIS_.START_DATE_TIME>=TO_DATE('22-06-2010','DD-MM-YYYY')
        AND THIS_.START_DATE_TIME<=TO_DATE('01-07-2010','DD-MM-YYYY')
        and pp3_.CAM_PRODUCT_GROUP='10'



This works without casting. The join part in red, is the part in question. You have to trim the CHAR column then it'll work. That's precisely my question :) How to do trim() in Criteria API? As you can see in OracleCHAR.java, I've tried creating a new UserType and call trim() in overriden nullSafeGet() but it doesn't work. Guess it only works for query the field, not joining it to another field.

I'm doing up a search page (like http://www.devx.com/Java/Article/28754/ ... portItem=1) so at the moment I don't want to use native SQL or HQL in my code, cos Criteria API is precisely created for functionality like "search" screens where there is a variable number of conditions to be placed upon the result set.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.