Deserialize joda time by line in grails?

I had a LocalTime field (using Joda Time) in the Grails domain class.

Class WorkDone{ LocalTime duration } 

Now I have changed this field to String (with text limitation) so that it can support a duration of more than 24 hours.

 String duration 

The problem is that the database already has some data. And I want to misinform this data through database migration in Grails. I use Postgres, which saves LocalTime as Bytea (binary data).

When I call WorkDone.duration, it returns me a form line:

\ xaced0005737200176f72672e6a6f64612e74696d652e4c6f63616c54696d65fffff44abbf29def0200024a000c694c6f63616c4d696c6c69734c000b694368726f6e6f6c6f677974001a4c6f72672f6a6f64612f74696d652f4368726f6e6f6c6f67793b78700000000000000000737200276f72672e6a6f64612e74696d652e6368726f6e6f2e49534f4368726f6e6f6c6f67792453747562a9c811667137502703000078707372001f6f72672e6a6f64612e74696d652e4461746554696d655a6f6e652453747562a62f019a7c321ae30300007870770500035554437878

How can I extract time from this line?

+6
source share
3 answers

Your data is copied in Hex format in bytea format (starts with \ x), see PostgreSQL docs

http://www.postgresql.org/docs/current/static/datatype-binary.html

You need to cancel it before reading how ObjectInputStream ang get the LocalTime object, cancel it, and then try again, as Raphael suggests.

+4
source

You had to migrate the data before changing the data type to String.

Here is what you should do. 1. Change the field data type to LocalTime. 2. Create a new field using String Date. 3. Write a script that will receive the entire date in LocalTime and convert it to String and save it in a new field. 4. After transferring the data, delete the old field and then rename the new field to duration.

+1
source

As a result, I did the following -

 grailsChange{ change{ sql.eachRow("Select id,duration from work_done"){ def wdId = it.id def durationObj = (LocalTime)(new ObjectInputStream(new ByteArrayInputStream(it.duration))).readObject() durationObj = durationObj.toString().substring(0,8) WorkDone.executeUpdate("update WorkDone wd set wd.duration=:newDuration" + "where wd.id=:wdId", [newDuration:durationObj ,wdId:wdId ]) } } 
+1
source

Source: https://habr.com/ru/post/958674/


All Articles