File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes trim in entityBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "trim in entityBean" Watch "trim in entityBean" New topic

trim in entityBean

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I have a CHAR-field in my database, this field has a fixed lenght:

@Column(nullable = false, insertable = true, updatable = true, length = 10, columnDefinition = "char(10)")
public String getName() {return name.trim(); }

I use the trim-method to get always the trimmed version of my name (that means all blanks will be deleted), when I retrieve this field from the database.

Should I do the trim in the getter or setter?

The problem is, when I do the trim in the getter, then JPA/Hibernate wants to update all these trimmed fields. I do not want that. I only want avoid to call everytime the trim-method from my session-Bean when I get char-fields. Any suggestions?
[ December 04, 2008: Message edited by: nimo frey ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1710
Hi Nimo,

Does data come from client side? Do you have validation code on server end?
If so, instead of trimming the String in the setter method itself, if you
put that inside validating method that returns desired trimmed string (or char
array), it would be good.

Lets see what others say here.

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
yes, exactly. I use server-side-validation:

So, doing trim in getter resolves to a validation-failure.

So I have to do the trim in the validator, also?

But I do not want to update every row only because of the trimmed version of the field and I do not want to clutter my session-beans with trims for every field..hmm
I agree. Here's the link:
subject: trim in entityBean
It's not a secret anymore!