I'm using ORMLite to persist objects in an SQLite Database, but persisting LocalDateTime values does't work. I already configured a custom persister class like this:
public class LocalDateTimePersister extends TimeStampType {
private static final LocalDateTimePersister singleTon = new LocalDateTimePersister();
String dateFormat = "yyyy-MM-dd'T'HH:mm:ss";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dateFormat);
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
private LocalDateTimePersister() {
super(SqlType.STRING, new Class<?>[] { LocalDateTime.class });
}
public static LocalDateTimePersister getSingleton() {
return singleTon;
}
@Override
public Object javaToSqlArg(FieldType fieldType, Object javaObject) {
LocalDateTime dateTime = (LocalDateTime) javaObject;
if (dateTime == null) {
return null;
} else {
return dateTime.format(dtf);
}
}
@Override
public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos) {
return LocalDateTime.parse((String) sqlArg, dtf);
}
}
It saves the value correctly, but loading the object causes this exception:
java.text.ParseException: Unparseable date: "2023-10-15T12:57:46.000" does not match (\p{Nd}++)\Q-\E(\p{Nd}++)\Q-\E(\p{Nd}++)\Q \E(\p{Nd}++)\Q:\E(\p{Nd}++)\Q:\E(\p{Nd}++)\Q.\E(\p{Nd}++)
The time value that was saved in the database is "2023-10-15T12:57:46", so I don't know where that string with the milliseconds value comes from. The breakpoint inside my javaToSqlArg method isn't hit and I can't see what's causing this error exactly.
Also, I noticed that there's no foreign key constraint being applied when creating the table, although the attributes are annotated appropriately. So child objects just stay in the database when I delete the parent object. Is there a way to change that without using a text query to create the table?
If you look closely at the generated pattern error message, I see
\Q \Ein the middle when I would have expected\QT\E. Are you sure that the pattern that generated the error was as you describe in the post?Any chance that your code really has:
If it does have the
Tthen I'm not sure what's going on. gray