views:

330

answers:

2

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^^

+1  A: 

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
Mehrdad Afshari
I do a .trim() in the setter but this has no effect
+1  A: 

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.)

Henning