0

In Database, We have size varchar(30).

From source we are getting data of 31 chars

In Hibernate, The field is mentioned like

   @Column("ColumnName")
   private String columnName;

We have around 100 to 200 such columns having varchar for different databases.

Is there a way to trim the data first and then set it to Hibernate setter or is there any way in hibernate to do so for say 150 out of 200 columns?

The approach i am following is to check database column length substring the same at setter something like -> len > 30? substring(0,30): len ; but is there any good solution to this or hibernate specific solution to this?

fatherazrael
  • 5,511
  • 16
  • 71
  • 155
  • You can use converters for such purpose I believe. Look [here](https://stackoverflow.com/questions/7646572/how-to-silently-truncate-strings-while-storing-them-when-they-are-longer-than-th) if it helps. There are other options as well which you may like. – Sujitmohanty30 Nov 04 '20 at 02:54

1 Answers1

0

As far as I know, Hibernate does not help you here, but in my opinion you shouldn't do this. If a user enters some string and you trim it down to a size, it might produce undesired results. You should handle the length restriction in the UI already and let the user decide what to do if the string he wants to enter doesn't fit the restrictions your column has.

Christian Beikov
  • 15,141
  • 2
  • 32
  • 58