When I try to update a record in the database via Dao error pops up:

  Exception in thread "main" java.sql.SQLException: Unable to run update-id stmt on object ImageRecord(imageEmulator=BOCHS, imageOSType=LINUX, imageType=IMAGE_BIOS, description=22222222222, name=aaaaaaaaaaaa, lastModified=Wed Jul 06 15:38:17 MSK 2016, id=3): UPDATE `images` SET `id` = ? WHERE `id` = ? at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22) at com.j256.ormlite.stmt.mapped.MappedUpdateId.execute(MappedUpdateId.java:51) at com.j256.ormlite.stmt.StatementExecutor.updateId(StatementExecutor.java:459) at com.j256.ormlite.dao.BaseDaoImpl.updateId(BaseDaoImpl.java:365) at com.dugin.rostislav.image.database.sqlite.SQLiteDBHelper.changeImage(SQLiteDBHelper.java:59) at com.dugin.rostislav.image.database.ImageStorageHelper.changeImage(ImageStorageHelper.java:14) at com.dugin.rostislav.Main.main(Main.java:21) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) Caused by: java.sql.SQLException: Could not assign object '3' to field FieldType:name=id,class=ImageRecord at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22) at com.j256.ormlite.field.FieldType.assignField(FieldType.java:560) at com.j256.ormlite.stmt.mapped.MappedUpdateId.execute(MappedUpdateId.java:42) ... 10 more Caused by: java.lang.IllegalArgumentException: Can not set int field com.dugin.rostislav.image.database.sqlite.ImageRecord.id to java.lang.String at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) at sun.reflect.UnsafeIntegerFieldAccessorImpl.set(UnsafeIntegerFieldAccessorImpl.java:98) at java.lang.reflect.Field.set(Field.java:764) at com.j256.ormlite.field.FieldType.assignField(FieldType.java:558) ... 11 more 

As a database object, I use this class ( @Getter and @Setter are Lombok library annotations, they generate getters and setters, and @ToString prints all the fields when calling .toString ):

 @ToString @DatabaseTable(tableName = TABLE_NAME) public class ImageRecord extends BaseDaoEnabled { @Getter @Setter @DatabaseField(columnName = COLUMN_IMAGE_EMULATOR_TYPE) private Image.ImageEmulator imageEmulator; @Getter @Setter @DatabaseField(columnName = COLUMN_IMAGE_OS_TYPE) private Image.ImageOSType imageOSType; @Getter @Setter @DatabaseField(columnName = COLUMN_IMAGE_TYPE) private Image.ImageType imageType; @Getter @Setter @DatabaseField(columnName = COLUMN_DESCRIPTION) private String description; @Getter @Setter @DatabaseField(columnName = COLUMN_NAME) private String name; @Getter @Setter @DatabaseField(columnName = COLUMN_LAST_MODIFIED, version = true, dataType = DataType.DATE_LONG) private Date lastModified; @Getter @Setter @DatabaseField(generatedId = true) private int id; //Auto generated DB column } 

The insertion happens like this:

 imagesDao.updateId(imageRecond, imageRecord.getId()); 

imageRecord is an object with fields:

 imageEmulator=BOCHS imageOsType=LINUX imageType=IMAGE_BIOS name=aaaaaaaaaaaa description=22222222222 lastModified=1467809082541 id = 1 

Why is the database trying to insert an id into some string field and how to fix it?

  • If your ID is spelled as generated, then try to leave it empty / equal to zero. And in general - why should I change the ID? ... - YuriySPb
  • @Yuriy SPb, I don’t change it ... - user189127
  • And what then does the updateId method accept the ID? .. - Yuriy SPb
  • @ YuriySPb, updates the record by the specified id, by meaning. - user189127 pm
  • one
    @ bukashka101, in general, it looks like json ... And you can first parse the elements between the brackets, then split the comma with a space, and then by = - YuriiSPb

1 answer 1

Something is wrong in the ID update method or in the data model or in the table. An error indicates that an int is needed and a String is received.

If the ID is known and is in the updated object, then the second argument is not needed. If the ID is unknown, even more so. In any case, changing the ID in the tables is a bad idea and you don’t need to. If there is a need for it, then you are doing something wrong.