Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Is there a simple way to trim() Strings as loaded from database into Entity?
Is there a simple way to trim() Strings as loaded from database into Entity? [message #381278] Tue, 09 September 2008 16:30 Go to next message
Bill Blalock is currently offline Bill BlalockFriend
Messages: 119
Registered: July 2009
Location: Alabama
Senior Member
I am working with a data base of log enteries. The String fields are
described at 255 fixed length. Some are empty. Most have only a few
meaningful characters at the front (user, IP adress, etc).

The entity being returned is populated with the full 255 character, mostly
blank.

Is there anyway to trim() the String fields as they are loaded into the
entity?

The reason I want to do this is that the program will be running queries
which return Lists of the entities. Each entity has several untrimmed
fields with mostly blanks and the querry returns several hundred records.

@Column (name="FOO")
String foo;
..
..
public String getFoo() {
return foo.trim();
}

Yes, the value returned is trimmed but entity.foo is 255 characters,
mostly blanks.

public void setFoo( String foo) {
this.foo = foo.trim();
}

Doesn't help, setFoo isn't called when the entity is made.

@ReadTransformer(method="trimFoo)

public String trimFoo(Record rec, Session ses) {
return ((String)rec.get("FOO")).trim();
}

might work but it seems like a lot of work.

Are there any other ways to do this?

Thanks!
Re: Is there a simple way to trim() Strings as loaded from database into Entity? [message #381279 is a reply to message #381278] Tue, 09 September 2008 18:00 Go to previous messageGo to next message
Bill Blalock is currently offline Bill BlalockFriend
Messages: 119
Registered: July 2009
Location: Alabama
Senior Member
I think I found what I was looking for but I have a couple of questions
(after code)....

@Entity
@Table(name="FTPLOG01")
@Converter(name="trimStringConverter",
converterClass=domain_model.TrimStringConverter.class )
..
@Convert("trimStringConverter")
private String clientHost;

@Convert("trimStringConverter")
private String username;
..
..
public class TrimStringConverter implements Converter {

public TrimStringConverter() {
// TODO Auto-generated constructor stub
}

@Override
public Object convertDataValueToObjectValue(Object dataValue,
Session session) {
return ((String)dataValue).trim();
}
@Override
public Object convertObjectValueToDataValue(Object objectValue,
Session session) {
return objectValue;
}
@Override
public void initialize(DatabaseMapping mapping, Session session) {
// TODO Auto-generated method stub

}
@Override
public boolean isMutable() {
// TODO Auto-generated method stub
return true;
}
}

Questions:

Should convertObjectValueToDataValue() return null since nothing will
change going back to the data base or should it return the object?

Should isMutable() return true or false. I don't understand this one.

I get these warnings, one for each field in the entity trimmed:
[EL Warning]: 2008.09.09 12:45:09.781--ServerSession(10217370)--Ignoring
default serialization on element [private java.lang.String
domain_model.Ftplog01.clientHost] within entity class [class
domain_model.Ftplog01] since a @Convert is specified.
[EL Warning]: 2008.09.09 12:45:09.796--ServerSession(10217370)--Ignoring
default serialization on element [private java.lang.String
domain_model.Ftplog01.username] within entity class [class
domain_model.Ftplog01] since a @Convert is specified.
[EL Warning]: 2008.09.09 12:45:09.796--ServerSession(10217370)--Ignoring
default serialization on element [private java.lang.String
domain_model.Ftplog01.service] within entity class [class
domain_model.Ftplog01] since a @Convert is specified.
[EL Warning]: 2008.09.09 12:45:09.796--ServerSession(10217370)--Ignoring
default serialization on element [private java.lang.String
domain_model.Ftplog01.machine] within entity class [class
domain_model.Ftplog01] since a @Convert is specified.
[EL Warning]: 2008.09.09 12:45:09.796--ServerSession(10217370)--Ignoring
default serialization on element [private java.lang.String
domain_model.Ftplog01.serverip] within entity class [class
domain_model.Ftplog01] since a @Convert is specified.
[EL Warning]: 2008.09.09 12:45:09.796--ServerSession(10217370)--Ignoring
default serialization on element [private java.lang.String
domain_model.Ftplog01.operation] within entity class [class
domain_model.Ftplog01] since a @Convert is specified.
[EL Warning]: 2008.09.09 12:45:09.796--ServerSession(10217370)--Ignoring
default serialization on element [private java.lang.String
domain_model.Ftplog01.target] within entity class [class
domain_model.Ftplog01] since a @Convert is specified.
[EL Warning]: 2008.09.09 12:45:09.796--ServerSession(10217370)--Ignoring
default serialization on element [private java.lang.String
domain_model.Ftplog01.parameters] within entity class [class
domain_model.Ftplog01] since a @Convert is specified.

What needs to be done now?

Thanks all
Re: Is there a simple way to trim() Strings as loaded from database into Entity? [message #381281 is a reply to message #381279] Wed, 10 September 2008 14:14 Go to previous messageGo to next message
James is currently offline JamesFriend
Messages: 272
Registered: July 2009
Senior Member
Return the value, not null, isMutable should be false (Strings are atomic).

This should work fine. In general EclipseLink will trim CHAR fields by
default, but not VARCHAR. It is odd that your DB is space padding VARCHAR.

The converter it probably best, but you could also annotate your property
get method instead of the variables in you want JPA to access your object
using the get/set methods (you have to annotate either all fields or all
get methods).

-- James
Re: Is there a simple way to trim() Strings as loaded from database into Entity? [message #381283 is a reply to message #381281] Thu, 11 September 2008 14:43 Go to previous messageGo to next message
Bill Blalock is currently offline Bill BlalockFriend
Messages: 119
Registered: July 2009
Location: Alabama
Senior Member
James wrote:

> Return the value, not null, isMutable should be false (Strings are atomic).

> This should work fine. In general EclipseLink will trim CHAR fields by
> default, but not VARCHAR. It is odd that your DB is space padding VARCHAR.

> The converter it probably best, but you could also annotate your property
> get method instead of the variables in you want JPA to access your object
> using the get/set methods (you have to annotate either all fields or all
> get methods).

> -- James

Thanks James!!!

The fixed character length fields were being trimmed. The VARCHAR fields,
defined as 255 VARCHAR, were not being trimmed. The DB is Microsoft 2005
SQL.

> ... but you could also annotate your property
> get method instead of the variables in you want JPA to access your object
> using the get/set methods (you have to annotate either all fields or all
> get methods).

I made this change but it doesn't work as I expected. For example:

Original:
@Convert("trimStringConverter")
private String clientHost;

clientHost was returned trimmed. Using the debugger I am looking at an
entity object in the List returned by the Query.getResultList()

I made the change I thought you were suggesting. I changed the annotation
from all the String fields to all the get methods.

private String clientHost;
..
@Column
@Convert("trimStringConverter")
public String getClientHost() {
return clientHost.trim();
}

clientHost was returned untrimmed, as a String with length of 255.
getClientHost() returns an untrimmed field.

One more question:

When the @Convert is specified on the field the log gives a warning
message for each field:

Ignoring default serialization on element [private java.lang.String
domain_model.Ftplog01.clientHost] within entity class [class
domain_model.Ftplog01] since a @Convert is specified.

The message goes away when the @Convert is moved to the method. Which is
okay except the @Convert doesn't appear to be doing anything at the method
level.

Thanks for your time.
Bill Blalock
Re: Is there a simple way to trim() Strings as loaded from database into Entity? [message #381387 is a reply to message #381283] Mon, 15 September 2008 14:25 Go to previous messageGo to next message
James is currently offline JamesFriend
Messages: 272
Registered: July 2009
Senior Member
Sorry, I think I confused you.

In my reply I outlined two separate solutions, either use converters OR
use get/set methods.

So, for converters do things just as you had with the @Convert on the
field, not method, and don't define the String get/set methods. This
should work, and is the best solution.

The get/set method option is just a generic JPA workaround that does not
use EclipseLink converters. It just uses property access to do the same
thing.

-- James
Re: Is there a simple way to trim() Strings as loaded from database into Entity? [message #381389 is a reply to message #381387] Mon, 15 September 2008 17:46 Go to previous messageGo to next message
Bill Blalock is currently offline Bill BlalockFriend
Messages: 119
Registered: July 2009
Location: Alabama
Senior Member
Hi James!

Thanks for making that clear, choice of get/set and Converter.

I thought to use Converters to throw out the extra space at the end of
several database fields (VARCHARs as discussed earlier) before they were
stored in the List collections returned by the execution of queries.

When using Converters I get warnings like this:
0 [main] WARN eclipselink - Ignoring default serialization on element
[private java.lang.Boolean domain_model.Xfrctl00.dueThursday] within
entity class [class domain_model.Xfrctl00] since a @Convert is specified.
-- ServerSession(17153368)
16 [main] WARN eclipselink - Ignoring default serialization on element
[private java.lang.Boolean domain_model.Xfrctl00.dueFriday] within entity
class [class domain_model.Xfrctl00] since a @Convert is specified. --
ServerSession(17153368)

Should I be concerned about them? Is there anything I need to do in my
code to compensate?

The String converter is the one we have been discussing (to trim VARCHAR
while reading).

This is the boolean converter:

@ObjectTypeConverter (
name="booleanConverter",
dataType=java.lang.String.class,
objectType=java.lang.Boolean.class,
conversionValues={
@ConversionValue(dataValue="1", objectValue="true")},
defaultObjectValue = "false"
)

In my database, booleans are stored as "1" for true. in CHAR(1) fields.

Thanks for your time James.
Re: Is there a simple way to trim() Strings as loaded from database into Entity? [message #381390 is a reply to message #381389] Tue, 16 September 2008 13:09 Go to previous message
James is currently offline JamesFriend
Messages: 272
Registered: July 2009
Senior Member
You can ignore the warning, it is just an informational message. It
should be fixed to no log at the default log level.

-- James
Previous Topic:enum collection
Next Topic:LINQ for EMF, experience so far
Goto Forum:
  


Current Time: Thu Apr 25 17:37:34 GMT 2024

Powered by FUDForum. Page generated in 0.03236 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top