How to reuse fieldlength in form, validation and ddl?

前端 未结 3 1928
情书的邮戳
情书的邮戳 2020-12-09 22:41

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 t

3条回答
  •  萌比男神i
    2020-12-09 23:25

    The solution provided by EJB reads the annotated value for column length. James McMahon pointed out that this exposes the Hibernate implementation. That can be resolved fairly simply with getter methods. Assuming there is an interface for Person with a declared method getFirstNameLength:

    import javax.persistence.Column;
    import javax.persistence.Entity;
    
    @Entity
    public class PersonImplementation extends Person {
    
       @Column(length=30)
       private String firstName;
    
       public int getFirstNameLength() {
           return this.getClass().getDeclaredField("firstName").getAnnotation(Column.class).length();
       }
    }
    

提交回复
热议问题