Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find out the length of a column in a JPA @Entity?

Tags:

java

jpa

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

like image 363
yottamoto Avatar asked Mar 23 '09 15:03

yottamoto


People also ask

What is column length?

The length of the roadway occupied by a column or a convoy in movement. See also road space. Dictionary of Military and Associated Terms.

Which is JPA entity properties?

Entity PropertiesPersistability - An object is called persistent if it is stored in the database and can be accessed anytime. Persistent Identity - In Java, each entity is unique and represents as an object identity.

What is column definition JPA?

In JPA Spec & javadoc: columnDefinition definition: The SQL fragment that is used when generating the DDL for the column. columnDefinition default: Generated SQL to create a column of the inferred type.


1 Answers

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()
like image 187
James McMahon Avatar answered Nov 08 '22 15:11

James McMahon