views:

128

answers:

2

I'm working on an Spring application with lots of input forms. I'd like to reuse the field length in the UI-form, validation and JPA annotations. Is there an elegant way to solve this. My solution at the moment is, to use constants to declare the length:

public class Person
{
   public static final int FIRSTNAME_LENGTH = 25;

   @Column(length=FIRSTNAME_LENGTH)
   private String firstName;

   ...
}

and then reuse the constant in the Validator and the Jsp

...

<form:input path="firstName" 
    maxlength="<%= Integer.toString(Person.FIRSTNAME_LENGTH) %>"/>

...

which is pretty verbose.

Is there any more elegant solution to this problem?

+1  A: 

I asked a similar question when I was using Hibernate,

Getting column length from Hibernate mappings?

No idea how to pull this information under JPA however, but if you are using Hibernate as your underlying layer you could try access Hibernate directly and implement the solution above.

However the Hibernate solution is just as in-elegant as the solution that you outlined in your answer, with the added disadvantage that it ties your JPA implementation to Hibernate.

James McMahon
+2  A: 

It's quite possible to access the information stored in annotations. In fact, this is their main purpose: storing meta information on a class/ method/ field. Here is an example of how to access the length stored in a @Column annotation:

import javax.persistence.Column;
import javax.persistence.Entity;

@Entity
public class Person {

   @Column(length=30)
   private String firstName;

   public static void main(String[] args) throws SecurityException, NoSuchFieldException {
      Object person = new Person();
      //find out length    
      System.out.println(person.getClass().getDeclaredField("firstName").getAnnotation(Column.class).length());
   }
}

You should be able to create some custom tag or bean to extract this info generically.

It's not difficult to create your own annotations. You could consider creating one that specifies which fields are to be included on the form, how they should be rendered, description, etc. Than you could create a generic form. Then again you may not like to mix domain and presentation.

EJB
That's quite a line full, the famous Java terseness is alive and well. Great tip, thank you.
James McMahon
Just a follow up, java.lang.Class.getDeclaredField() throws two checked exceptions, NoSuchFieldException and SecurityException. This makes this method a little more clunky and is something to keep in mind if your are trying to implement this solution.
James McMahon