You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by DonBrady <db...@pobox.com> on 2006/10/29 02:37:50 UTC

Trimming Strings from Database Query Results

Hi,

 If this is not an appropriate question for this list, please let me know
and I am sorry.

 I am converting a large application from toplink-essentials to openJpa.  

 I do have it running now under WebSphere 6.1 accessing DB2.  I really like
openJpa!

 I had to specify

        <property name="openjpa.jdbc.DBDictionary"
value="db2(StoreCharsAsNumbers=false)"/>

to avoid exceptions retrieving character values.

I am finding that all strings had trailing blanks trimmed when retrieved in
toplink, whereas the trailing blanks are preserved in openJpa.  I can add
the many trim methods needed to my code if necessary, but I was wondering if
there is any way to do this via a property setting.  I could not find any.

Thanks!

Don
-- 
View this message in context: http://www.nabble.com/Trimming-Strings-from-Database-Query-Results-tf2532395.html#a7056365
Sent from the open-jpa-dev mailing list archive at Nabble.com.


RE: Trimming Strings from Database Query Results

Posted by Patrick Linskey <pl...@bea.com>.
>  Thank you so very much!
> 
>  I should have been able to figure that out myself I know.  I 
> was headed in
> the right direction but too slowly.

No worries... I didn't actually compile the code or anything, so I'm
glad it kinda worked.

>  I have now also added an override for getChar:

Looks like it should do the right thing.

-Patrick

RE: Trimming Strings from Database Query Results

Posted by DonBrady <db...@pobox.com>.
Patrick,

 Thank you so very much!

 I should have been able to figure that out myself I know.  I was headed in
the right direction but too slowly.

 I have now also added an override for getChar:

package com.x;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.lang.StringUtils;
import org.apache.openjpa.jdbc.sql.DB2Dictionary;

public class ExtendedDB2Dictionary extends
		org.apache.openjpa.jdbc.sql.DB2Dictionary {

	@Override
	public String getString(ResultSet rs, int col) throws SQLException {
		String str = super.getString(rs, col);
		if (str != null)
			str = str.trim();
		return str;
	}

	@Override
	public char getChar(ResultSet rs, int col) throws SQLException {

		char c = super.getChar(rs, col);
		if (c == 0)
			c = ' ';
		// make more TopLink compatible
		return c;
	}

}

-- 
View this message in context: http://www.nabble.com/Trimming-Strings-from-Database-Query-Results-tf2532395.html#a7106383
Sent from the open-jpa-dev mailing list archive at Nabble.com.


RE: Trimming Strings from Database Query Results

Posted by Patrick Linskey <pl...@bea.com>.
> I am finding that all strings had trailing blanks trimmed 
> when retrieved in
> toplink, whereas the trailing blanks are preserved in 
> openJpa.  I can add
> the many trim methods needed to my code if necessary, but I 
> was wondering if
> there is any way to do this via a property setting.  I could 
> not find any.

Hi,

The untrimmed fields are String fields, not char fields, correct?

There aren't any property settings to tell OpenJPA to trim String
fields, but I believe that you could implement something that did this
quite easily by extending the DB2Dictionary and overriding the
getString() method to do something like so:

package com.example;

import java.sql.ResultSet;

public class TrimmingDB2Dictionary 
    extends org.apache.openjpa.jdbc.sql.DB2Dictionary {

    @Override
    public String getString(ResultSet rs, int col) {
        String str = super.getString(rs, col);
        if (str != null)
            str = str.trim();
        return str;
    }
}

Then, you'll change your DBDictionary property like so:

    <property name="openjpa.jdbc.DBDictionary"
value="com.example.TrimmingDB2Dictionary(StoreCharsAsNumbers=false)"/>


-Patrick
_______________________________________________________________________
Notice:  This email message, together with any attachments, may contain
information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated
entities,  that may be confidential,  proprietary,  copyrighted  and/or
legally privileged, and is intended solely for the use of the individual
or entity named in this message. If you are not the intended recipient,
and have received this message in error, please immediately return this
by email and then delete it.