This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

trim in entityBean

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic