Title: How to remove trailing whitespace in a database column?
Tags: hibernate;validation;data-binding;jpa
Question: I have an EJB 3 Entity bean that has a String property annotated with: @Column(unique=true). This value is set from a JSF page.
Everything works fine as long as the user does not add whitespace add the end of the input field. In that case "someName" and "someName____" are treated not unique and are stored in the database. Is there a convenient way to remove trailing (and leading) whitespace in application land, or has this to be done in database land?
There is an SQL Server 2008 at the end of the line and I use Hibernate for ORM.
Re-Publishing your code to the dev server works wonders Sorry^^


Here is another answer: If you don't want to modify the application to trim the string before submitting it to server. You can either:
If you are using stored procedures to insert data, modify the stored procedure and make it trim the string
Use a trigger to trim the string
Comment for this answer: I do a .trim() in the setter but this has no effect

Here is another answer: Since, as you say in your comment to Mehrdad's answer, you have control over the source code, I'd strongly suggest you find out what's wrong with your call to trim() instead of just hacking your way through.
Are you maybe doing
public void setMyString(String myString) {
myString.trim();
this.myString = myString;
where you should be using trim()'s return value? (Remember Java Strings are immutable.)
public void setMyString(String myString) {
this.myString = myString.trim();
(Please add null checks as necessary.)

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2009:00:02/27/09', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<hibernate><validation><data-binding><jpa>', 'Title': 'How to remove trailing whitespace in a database column?', 'ViewCount': 3128, 'id': 594029, 'user_AboutMe': '', 'user_AccountId': 19570.0, 'user_DisplayName': 'raupach', 'user_Reputation': 3072.0, 'user_Views': 369.0, 'user_id': 132162.0}}