• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

trim in entityBean

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic