简体   繁体   中英

How to convert varchar to java.sql.Clob

Previously I had a data base column name called summary and it's type was varchar

private String summary;

In recently i wanted to change this column data type into TEXT to enable store large data.To do that i have used lob annotation in hibernate .

@lob
private String summary;

As a result of that i can store large amount of data to that summary field.But the problem is that i have some old records that summary field contains varchar. When get the data set it throws an error saying An attempt was made to get a data value of type 'java.sql.Clob' from a data value of type 'VARCHAR' .So my questions are,

  1. Do i need to delete my old records to solve type mismatch?
  2. Is it possible to keep old records with new data type?If so how can i achieve that?
  3. What is the best way to store large text in hibernate?

Please if anyone knows drop your answer.

The easiet way to migrate from string to clob:

1- add an intermediate attribute like this

@lob private String summaryAux;

and delete @lob from the summary attribute

2- use a java main function that get all the records and set the "summary" into the "summaryAux".

main(){
List<Record> records = getAllRecors();
for(Record record: records){
record.setSummaryAux(record.getSummary());}
saveRecords(records);
}

3-delete the

@lob private String summaryAux;

from your code and put @lob again on private String summary; and also delete summary column from the the associated table and finally rename summaryAux column => summary

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM