tags:

views:

150

answers:

3

I've got a String column defined with

@Column(length = 4000)

The attribute contains log information and can be longer than the defined length. In this case, the field can be safely truncated without throwing an error message.

I'd like to find out the determined length of the column in the DAO class for the pojo so that I can truncate the string if necessary. How can I do that?

Regards,

Chris

A: 

Using Reflection would be an obvious answer.

Are you using Hibernate? If so then the Hibernate Validator should validate the length for you and provide a suitable error message.

Damo
I'm using Hibernate and have considered using Validator but I don't think it would be appropriate. The column contains log data which can safely be truncated without throwing an error.
cecube
Then Reflection would be the best way. There's no easy Column.getLength() method unfortunately.
Damo
How would you go about reflecting this?
James McMahon
+1  A: 

Well, the easiest way to do it would be using reflection with your annotations. I won't be cheap though, in terms of performance.

Pablo Santa Cruz
That's what's in place now. I just thought that as a proper OR-Mapper, you could easily access field metadata.
cecube
A: 

Check out EJB's answer to this question;

How to reuse fieldlength in form, validation and ddl ?

To paraphrase, if you wanted the length from the firstName column in the person table, you use this code.

person.getClass().getDeclaredField("firstName").getAnnotation(Column.class).length()
James McMahon